Live data from Hacker News

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

markodenic.com

21–30 of 156 posts

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

#21
Another tip I found very useful lately

You can use `$("selector")`, even without jQuery, in console. (not sure if with firefox, works with safari and chrome)

And also `$x("path")` for xpath.

But note, this works only in console, it’s not available from javascript.

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

#22
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));

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

#24
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 they were when you printed them. But the unexpanded view shows them as they were. So for example:

    h = {foo: "bar"}
    console.log(h)
    ▶ Object { foo: "bar" }
    h.foo = "BAR"
Then you click the triangle and you see:

    ▼ {..}
    |   foo: "BAR"
    | ▶ : Object { .. }
I don't know if that's a bug or desired behavior, but watch out for it! In the past I've used console.log(JSON.stringify(h)) to capture the full object as-is. I guess turning it back into an object would be even nicer, so you could have a deep copy to navigate.

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

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

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

#27
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.

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

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

#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 memory leaks.

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

#30
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.

Posted this and then realized you beat me to it :)
Post reply on HN