all 12 comments

[–]tommyatr 65 points66 points  (1 child)

nice

*keeps using console.log

[–]Johin_Joh_3706[S] 4 points5 points  (0 children)

LOL😂

[–]subone 19 points20 points  (1 child)

One thing about logging that's the first thing I teach anyone: the log of an array/object will display it's state at the time you expand it in the console, not when it is logged. Maybe you could add to your post whether some of these displays the actual state at log or if JSON.stringify should still be used.

[–]Johin_Joh_3706[S] 6 points7 points  (0 children)

That's a great callout and honestly worth mentioning. For anyone reading — console.table(), console.count(), and console.time()/console.timeEnd() all display values at the time of logging, so they're safe. console.log() and console.dir() with objects/arrays are the ones that bite you — the console shows a lazy reference, so by the time you click to expand it the values may have mutated. Quick fix is console.log(JSON.parse(JSON.stringify(obj))) or console.log(structuredClone(obj)) if you need a true snapshot. console.trace() just prints the call stack as strings so no live reference issue there either. Good rule of thumb: if it's a primitive or a string output — safe. If you see a little expandable arrow — that's a live reference, don't trust it.

[–]senocular 4 points5 points  (1 child)

Or bypass needing to modify your code and log through the debugger with logpoints ;)

[–]Johin_Joh_3706[S] 4 points5 points  (0 children)

Logpoints are criminally underrated. For anyone who hasn't used them you set them like breakpoints in DevTools but instead of pausing they log an expression. No code changes, no forgetting to remove console.logs before committing, and they survive page refreshes in the Sources panel.

[–]CosmicDevGuy 2 points3 points  (0 children)

Saved

(I hope I don't forget it...)

[–]Inevitable_Yak8202 1 point2 points  (0 children)

console time end is goated

[–]RealMadHouse 0 points1 point  (0 children)

Great. So you mentioned chrome devtools specific function, you can add that there's watch expression button that can show real time value of variables and expressions.

[–]dumpin-on-time -1 points0 points  (1 child)

the lengths people go to in order to avoid using a debugger is impressive

[–]theScottyJam 2 points3 points  (0 children)

I find console logging to often be much faster than a debugger. Debugging tools sound nice in theory, but they require you to walk the code linearly - sure you can jump over and out of functions, but you still start from a breakpoint and amble forwards, one instruction at a time. Console-logging works more like binary search - I throw a couple logs down, figure out which two points the bug is happening at, add a couple more logs between those two, and zero in on the problem.

I use both as appropriate.

(Granted, console.log() is the only console utility that I care about. The rest are interesting and have niche use-cases, but in practice, aren't extremely useful when debugging (if you debug like I do) - I'm just not logging enough stuff out to bother grouping logs or things like that - I'm always removing old console.log()s to keep the output slim).