0

When I use console.log to debug I typically use a line of code that looks like this.

console.log("totalLength " +totalLength);

My output includes the variable name and its value.

But is there shorthand for this? Or is there maybe a better/faster/more convenient line of code to monitor variables? Thanks so much!

DR01D
  • 1,325
  • 15
  • 32
  • The nearest I can think of is put all the variable to be debugged in an object, code might looks better however it doesn't make it shorter than this – AngYC Nov 05 '17 at 22:26
  • You can use `%s` to create a format string and supply variable values as arguments. E.G. `console.log("The total length is %s", totalLength)`. Works well with multiple variables as well. – traktor Nov 05 '17 at 22:27

2 Answers2

4

Yes, it's called using the debugger. Printing out variable values is not a good debugging practice.

If you are using Chrome, open up the Developer Tools and go to the Source tab. In the sidebar take a look at "Watch" and add your expressions in it.

enter image description here

You can also just look at source code panel. It will show all the variables' value by default. The "Watch" section allows more flexible expressions.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

You can monitor all the variables present in the global scope.

for(var b in window) { 
  if(window.hasOwnProperty(b)) console.log(b); 
}

The window object contains the information about the functions and vars present. You can essentially type window in console and monitor.

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47