Live data from Hacker News

Use console.log() like a pro (2020)

markodenic.com

1–10 of 156 posts

Re: Use console.log() like a pro (2020)

#2
Another trick that might be more practical for actually debugging is using the object shorthand. For example, instead of...

    console.log(x, y);
which contains the information you need, but lacks any useful context, try...

    console.log({x, y});
...which will print out like an object, including the key names.

Re: Use console.log() like a pro (2020)

#9

I often always do this to deep print my javascript object. console.log(JSON.stringify(myObject, null, 4));

This is great trick because the console will keep a reference to your object, not a copy, so if it changes between the time it was logged and now then the log expando shows the new value, not the old value.

By printing out the object you get a point-in-time snapshot rather than a reference to a mutable object.

Re: Use console.log() like a pro (2020)

#10
If you're using console.log to do debugging then it's worthwhile giving yourself a little more data to point at where a problem might lie - timings.

Open up devtools (cmd+option+j), then open the command palette (cmd+shift+P), and then search for "console", and then select "Console - Show Timestamps". Now every console output will have the high definition timestamp prepended to it. That can be really helpful if you don't want to go down the whole perf chart rabbit hole, or if you think things might be running in the wrong order due to some async weirdness.

(This probably only works in Chrome)

Post reply on HN