The console log can send you information silently
By now, you have noticed all of the
console.log() commands in the code examples.
Usually there is a menu item that will display the console log.
- In Chrome, it is in the
- In Firefox, it is in the
- In older versions of Internet Explorer it is often, well, unreliable.
Run some code with the console open to see how it can help you debug things. It is like using alerts that you can keep buried in your code that nobody but you (and other developers) will see.
I recommend adding
- at the beginning of each function
- inside of each loop
- any time that you have to confirm a critical calculation
You can also type variable names and commands directly into the console, and it will report their values back to you.
console.log("numOpponents=" + numOpponents);
will print out the current value of numOpponents to the console log.
I am not making console logging mandatory, but you might be crazy not to use it. It makes debugging much more pleasant!
Discover what values your variables hold without
One of the things that it allows you to do is type in variable names so that you can see what is inside.
After you run a piece of code, try typing in the name of your variable directly into the console to see what that variable holds. Powerful!