Here are some effective ways of inspecting variables used in your code using browser dev tools to your advantage.
This is what we typically do in the code to know what exactly is happening with our variables.
|
|
This will have the following beautiful output in browser console.
Let’s dig deeper.
Always use commas
Instead of a plain string pass variables separated by comma in console
. This will present a navigable tree instead of an undecipherable object
.
|
|
You can do the same for multiple variables.
|
|
Use Indentations in console
|
|
Use tables
console.table
works better if you have limited data that you want to see in tabular format. If you have too many elements, you may rather want to stick to the navigable tree provided by the ‘normal’ console
.
|
|
Call attention using colors
If you are displaying select objects like a HTML element, use console.dir()
instead of console.log()
. This will present you a HTML type output instead of a JSON type output.
For statements that seek attention, try other console
options.
|
|
Or, better still - use asserts to highlight statements only when required.
|
|
Using warn and error is also a quick way to force a trace (you can expand variables in Dev tools to see details). You can also do the same by using a console.trace()
.
Style console statements
Apply CSS styles to the output string as specified by the second parameter in console
statement
|
|
Use count() instead of temporary variables
You may find yourself using a temporary variable to display counts within loop. Use count()
instead.
|
|
Use time() to measure time!
Use console.time()
to measure time for specific blocks of code.
|
|