What is console.log() for?

console.log() is used primarily for debugging, AKA checking the value of variables, and making sure your code is working as expected. It is one function that JavaScript developers use A LOT while coding. If you aren’t sure of the value of a variable, you can put a console.log() right beneath it, to see what it is or make sure it is what you expect it to be. For example:

var time = new Date().getHours();
console.log("time: ", time);

var a = 10;
var b = 12;
var sum = a + b;
console.log("sum: ", sum);

console.log() is a function that can take many parameters (remember to always separate parameters with a comma). In the examples above I gave it two parameters:

  1. A string to identify the variable name I am checking
  2. The variable whose value I want to check

It is always helpful to see output in the console but it won't happen automatically if you don't have any errors. So, it is common to use console.log(yourVariableHere); to make sure everything is working as expected.

Then, when you are sure everything is working as expected, remember to get rid of any console.log() so it doesn’t clutter up your beautiful working code!

Still need help? Contact Us Contact Us