Live data from Hacker News

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

markodenic.com

41–50 of 156 posts

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

#41
post #28

I've put over 200 tips like that on my website: https://umaar.com/dev-tips/ Each tip has a textual explanation, and an animated gif if you're a visual learner (I know, I need to scrap gifs and move to regular videos). There's a lot of tricks there which can hopefully improve your development and debugging workflows. Let me know if there are specific things you'd like to see. A few people have asked for how to find me…

Amazing! Thanks so much for this.

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

#42
post #38

My whole personal site[1] is one big console.log(), right down to theme matching :D Unfortunately I'm not sure anyone has actually noticed. 1: https://itsokayitsofficial.io/

I really like how that plays out as a sort of text only alternative to a site, very cool ;)

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

#43
post #39
post #27

Earlier quoted context omitted.

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.

I was just about to mention this, I wasted a solid hour or two on this until I realised what was happening.

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

#45

Earlier quoted context omitted.

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

I think you misunderstood the intent of the GP

But this way is easier to reconcile the output, because the values logged shown are what they where at the time of the console.log(), not at the time of expansion (later).

Try this in a browser console: x={a:1,b:{c:1}};console.log(x);x.b.c=2;

then 'expand' the object, 'c' will be logged as 2, not 1

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

#46
post #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 d…

In Firefox, you click on the gear icon in the top right of the console, then check "Show Timestamps".

Though you probably want the per-tab Console there (ctrl-shift-k). ctrl-shift-j would give you the multiprocess browser console, which is very noisy.

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

#47
post #25

Rather than using %s, %i, %o, %f, etc. You can use a templated string, like this: `This is a string that has been printed ${someVar} times.`

That works great for strings / numbers, not so much for objects (unless you really want to see "[object Object]" in your debug strings).

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

#48
post #28

I've put over 200 tips like that on my website: https://umaar.com/dev-tips/ Each tip has a textual explanation, and an animated gif if you're a visual learner (I know, I need to scrap gifs and move to regular videos). There's a lot of tricks there which can hopefully improve your development and debugging workflows. Let me know if there are specific things you'd like to see. A few people have asked for how to find me…

[deleted]

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

#49
post #26

Wow there are some cool tricks here! In Firefox any objects you pass to console.log are expandable, so you can say console.log("my hash", h). It seems to behave the same when you say console.log("my hash %o", h). But there is a tricky thing that has really confused me in some debugging efforts: when expanded, the object display is "live", so it always shows the current properties of the object, not the properties as…

That's an anti-feature. When you log something in traditional sense you are recording something at that time.

It's more that the way that the console reflects how the language works, and the console is not a log, it's a REPL (so console.output would have been a nicer name than console.log).

I think it would be more confusing if the console did not work like the rest of the language does.

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

#50

Wow there are some cool tricks here! In Firefox any objects you pass to console.log are expandable, so you can say console.log("my hash", h). It seems to behave the same when you say console.log("my hash %o", h). But there is a tricky thing that has really confused me in some debugging efforts: when expanded, the object display is "live", so it always shows the current properties of the object, not the properties as…

console.log can be an async function so it's not exactly a bug.

There's a more in-depth answer SO: https://stackoverflow.com/a/23392650

Post reply on HN