During debugging you might pepper your code with lines like these:
console.log('foo = ' + foo + ', bar = ' + bar)
I recommend to use variable interpolation instead:
console.log('foo = %o, bar = %o', foo, bar)
This has some advantages:
- It's easier to write
- Variables are colored in the console output
- You don't need to stringify arguments so the
+
operator doesn't explode - If the variable is a structured object like a DOM element or an object has, you will be able to expand it inside the console and look at its contents
Another concise way to write the statement is this:
console.log({ foo, bar });
This uses Javascript's new destructuring syntax Show archive.org snapshot and is actually shorthand for:
console.log({ foo: foo, bar: bar });
Like above, you will be able to expand this object inside the console and look at its contents.
Posted by Henning Koch to makandra dev (2016-01-30 16:32)