Live data from Hacker News

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

markodenic.com

31–40 of 156 posts

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

#32
post #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.

Unfortunately that tends to print the object with collapsed values, requiring you to expand the object to actually see any of the values.

If they're primitives most consoles will show you the values without expansion

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

#35
Another little trick; instead of doing:

  console.log("some label: " + JSON.stringify(someObj))
pass it as a separate parameter:

  console.log("some label: ", someObj)
and you'll get interactive expansions/manipulation in the console

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

#36
post #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.

Or you can console.log(JSON.stringify(x, null, 2));

I think you misunderstood the intent of the GP

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

#37

Not gonna lie, read this hoping to pat myself on the back for being a pro, twist: learned some cool stuff, console.memory()? Neato!

Another! If you inspect a console.logged function, DevTools shows you the scope it has access to.

https://umaar.com/dev-tips/117-inspect-function-scope/

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

#39
post #27

Earlier quoted context omitted.

Unfortunately that tends to print the object with collapsed values, requiring you to expand the object to actually see any of the values.

I much prefer it being collapsed. When dealing with large objects, the firehose can be annoying.

Then, one day, after 12 hours chasing a race condition bug, you learn that when you expand objects from console.log, it shows you the current value of properties and not the values at the time of the console log. Because the values are evaluated when you click on the arrow.

Then you quit web development and start a new career.

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

#40
post #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.

Yes, I love this one!
Post reply on HN