Who hasn't peppered their code with console.logs in an attempt to find that pesky bug? The logs can get daunting and confusing. These will help you...
For further actions, you may consider blocking this person and/or reporting abuse
Great article, I had never heard on console.assert!
One of the things that I starting doing as a short hand for writing labels is wrapping the variables in an object literal. It saves on the repetitive typing!
Wow! This great! I will definitely be using this from now on. Thank you 🙂
I like that trick too! And now with console.table you can make it look even better!
I like these things because they're just about as simple as
console.logbut more useful in a lot of cases.I have a hard time adopting debugging tools any more complicated than print statements.
Great post!
Totally agree! Very simple tips that can be used right away.
Thanks, Ben.... so much feels here!
It feels so good when someone else acknowledges things like this, really:
"I have a hard time adopting debugging tools any more complicated than print statements"
This 'validates' for those of us who still fumble around semi-blindly with the classic duo of console.log() and alert(stuff) :D
It is also possible to use console.log with JSON.stringify (some Object, null, 2)

A good use case for JSON.stringify is that in some cases console.log does not print the true value of its arguments.
So wrapping the argument in JSON.stringify prints the actual value.
It does print the true value, if you log a variable and then reassign it later the console log will show the original value. However properties of that variable may have changed since the log statement was made and the console only evaluates properties when you expand an object in the console, so you get the current values instead of those at the time you called console.log.
If you're interested in shallow properties you can just log
{ ...obj }instead ofobj, or if you want the entire tree of properties you can use something like Lodash'scloneDeepto get a copy of the object and all nested properties.npmjs.com/package/lodash.clonedeep
Could you run this code in a web debugger?
What output do you get?
{b: {a:false}}, which is as expected.Must have been the wrong example.
Don't forget about
console.dir! 🙂I often use console.dir to see the DOM attributes of an element
Great post, Leira! I love seeing more people use styles in their logging. I'd like to add one more to your list, console.tap. It's one of my own creation, made to solve the fact that
console.logreturns undefined. Insteadtapreturns the first value you passed in. Imagine trying tologthe result of thefilter:This is fascinating and addresses a frequently encountered awkwardness of console.log, thank you! I'm going to start using this a lot
I will definitely check it out. Thank you!
You forgot the most important console method:
Which creates a collapsible tree for hiding and showing more or less information.
Also console.time and console.timeEnd to track how long something took.
I’m going to have to start using groups. They will definitely make my console cleaner! Thank you
Dont forget about
console.time()andconsole.timeEnd()for performance tracking.console.traceto log track trace and many others cool features :)console.tracewas just the one I wanted to mention. Whenever I jut want to trace call stack to just see the flow of function calls better.CSS in console.log
console.log('%cHello World!', 'font-weight: bold; font-size: 50px; color: red; text-shadow: 3px 3px 0 rgb(217,31,38), 6px 6px 0 rgb(226, 91,14), 9px 9px 0 rgb(245,221,8), 12px 12px 0 rgb(5,148,68), 15px 15px 0 rgb(2,135,206), 18px 18px 0 rgb(4,77,145), 21px 21px 0 rgb(42,21,113)');('%cHello World!', 'font-weight: bold; font-size: 50px; color: red; text-shadow: 3px 3px 0 rgb(217,31,38), 6px 6px 0 rgb(226, 91,14), 9px 9px 0 rgb(245,221,8), 12px 12px 0 rgb(5,148,68), 15px 15px 0 rgb(2,135,206), 18px 18px 0 rgb(4,77,145), 21px 21px 0 rgb(42,21,113)');Console.group and console.groupEnd is also very useful.
You can use more than one
%cin a console string, and use''to reset styles e.g.console.log('%cvalue%c = %s', 'font-weight: bold; color: red;', '', str)If you want some very fancy output you can use this package, which supports multi-line styled output including inline objects and HTML elements:
github.com/astoilkov/console.message
You can even use the CSS trick to display images in the console!
github.com/adriancooney/console.image
I had no idea, now I won’t forget! Thanks for sharing!
Just be aware of support though. We found that console.dir for example wasn't supported by a couple of browsers a year or so back and had to write polyfills! Only an issue if you are baking them into the app (i.e. error handling methods) rather than debugging in devtools
Oddly console.dir is supposedly widely supported even in ie9, but we found this wasn't always the case...
Checkout the Colored Console log extension for VSCode.
marketplace.visualstudio.com/items...
You can have Flair on all console.log with just a keypress. Also, you can change the styling directly in the extension.
This seems promising. I will try it. Thank you
Had no idea this was possible, great post Leira
The CSS one was new to me haha, I see some nice troll error messages for my teammates coming in the future
I like the console.table() as every now and then we deal with json response. This could come handy as it is readable.
I prefer using the debugger over the console.log. This way I can control the steps I want to see from the debugger & most importantly I can see the call stack.
All of this information is blowing my mind, especially the fact that you can style console outputs!
I’m glad you find it useful!
Good work. Very helpful and informative.
congratulations for sharing Leira
console log with css, i like that
Console.table is one I wasn't aware of before. I'll definitely use that one.
Thanks for posting will help me with my exploring, debugging and testing quickly whilst learning new JavaScript concepts etc.
Thankful
The article was very interesting
I now prefer log points in Chrome Dev Tools. It was already possible before with conditional break points but not as easy as the log points :)
I have never tried log points. I will look into them. Thank you
I love nerdy console statements
Thanks, till now I only userd console.log, now I'm going deep with console.table ;-)
Long live “console.log”
These are great tips! Thanks for sharing 😄
Is there a css for python's log/command terminal?
I haven't learned Python yet so I can't help you. Hopefully another reader is able to answer this question!
Great post! I didn't know about most of these. Very helpful!
Article is very helpful.
console.error also useful for debugging.
This is a game-changer. I have never actually thought of looking up the console documentation until I read your article. This is very cool.
Don't forget about console.time
Great when pairing it with console.timeEnd() !
Never knew about console.table, thank you!!!
Console.time(label) and console.timeEnd(label)
Are useful to count how long an operation has taken, for performance tuning