DEV Community

Cover image for Say no to console.log!

Say no to console.log!

Alish Giri on June 17, 2024

Do you always use console.log in your projects during development? And even though we will keep using console.log, there are other alternatives th...
Collapse
 
oculus42 profile image
Samuel Rouse

Great list! When I'm debugging I often put a console.trace() within a console.group(). This lets me provide identifying information at the group label, and be able to open the group to get to the detailed trace information without it blowing up the console.

Collapse
 
alxwnth profile image
Alex

Haven’t heard about console.trace(), that’s a gem! The more you know…

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

There are many like

  • console.error
  • console.assert()
  • console.trace()
  • console.groupEnd()
Collapse
 
modelair profile image
modelair

the article of 2024. 🤦‍♂️

Collapse
 
allanbonadio profile image
Allan Bonadio

There's also console.info() which is increasingly almost exactly the same as console.log(). Yes, now it seems, exactly the same on Chrome. Gets a blue icon in Safari. And gets a ⓘ icon in Firefox. (I usually use emojis to mark different log msgs - more variety.)

Collapse
 
thaisavieira profile image
Thaísa Vieira

Great tips, I love them and I'll start using them every day.

Collapse
 
retrop5 profile image
retrop5

Loved it! The console.table is really helpful!
I am surprised this existed

Collapse
 
ice_ profile image
Ayz Abdulwahid

this is very helpful, thank you!

Collapse
 
forever585 profile image
Forever585

Thank you for your useful tips

Collapse
 
litlyx profile image
Antonio | CEO at Litlyx.com

This so cool!

Collapse
 
mdrejon profile image
Sydur Rahman

Really interesting

Collapse
 
thatcoolguy profile image
dami

Thanks for this

Collapse
 
praneshchow profile image
Pranesh Chowdhury

Wow, that's cool. I saved it.

Collapse
 
girordo profile image
Tarcísio Giroldo

Really useful!

Collapse
 
vgeruso profile image
Victor Geruso Gomes

nice!

Collapse
 
giovanni_filomena_a775b19 profile image
Giovanni Filomena

Very helpful. 💪🏼

Collapse
 
kukicivan profile image
kukicivan

Cool! Didn't use console.dir() and console.table(). It looks very useful!

Collapse
 
ayushh profile image
Ayush Sharma

nice

Collapse
 
stefania_barabas profile image
Stefania Barabas

Wow! Super useful, thanks for sharing! :D

Collapse
 
devoskar profile image
Oskar Pietrucha

I just prefer to stop with a breakpoint and observe elements in the DevTools

Collapse
 
alishgiri profile image
Alish Giri

Yeah, that is also a great way to debug. Thanks for mentioning Oskar.

Collapse
 
giovannimazzuoccolo profile image
Giovanni Mazzuoccolo

There are many more, maybe you can make a follow up article? :)

Collapse
 
alishgiri profile image
Alish Giri

Yeah sure! Thanks.

Collapse
 
rudolfolah profile image
Rudolf Olah

The profile function is great: developer.mozilla.org/en-US/docs/W...

Collapse
 
greggcbs profile image
GreggHume

I did not know about console.dir, thats super cool cause you know sometimes the terminal shows [Object] or [Array]

Collapse
 
alishgiri profile image
Alish Giri

Yes, it does. Glad it helped Gregg!

Collapse
 
greggcbs profile image
GreggHume • Edited

Actually i am wrong, it still logs out Array and Object, so a pretty useless log for nested data:
console dir output

Collapse
 
alishgiri profile image
Alish Giri • Edited

I guess you are logging your data in a terminal and not a browser. So if it helps, I actually stringify my data using JSON.stringify(your_data, null, 2) and use console.log() on that converted data in cases like these. Finally, you can then copy and paste that log in VSCode to format it depending on the size of the log.

Collapse
 
rifatparadoxical profile image
RIFAT PARADOXICAL

Console.dir not working same here :(

Collapse
 
alishgiri profile image
Alish Giri

Are you using this on a backend?

Collapse
 
rifatparadoxical profile image
RIFAT PARADOXICAL

No. Frontend 🙂

Collapse
 
lyzab profile image
Elizabeth Ajileye

It should work, actually. Tried all of them, and they worked perfectly.

Collapse
 
master_aless profile image
Jhon Alessandro

I'm done with console. log

Collapse
 
alishgiri profile image
Alish Giri

haha nice!

Collapse
 
jahid6597 profile image
MD. JAHID HOSSAIN

debugger; statement in our code to pause execution and inspect the state of variables and execution flow directly within our browser's developer tools or IDE debugger.

Collapse
 
alishgiri profile image
Alish Giri

Nice. Thanks for sharing!

Collapse
 
afolabiolajide profile image
Afolabi Olajide Samuel

Remembering to use all of this when working on a project might be impossible but I found this really helpful especially the console.time() & console.timeEnd()

Collapse
 
alishgiri profile image
Alish Giri

Yeah lol I can relate. Glad it was helpful! Thanks.

Collapse
 
aatmaj profile image
Aatmaj

Thanks! ❤️

Collapse
 
alishgiri profile image
Alish Giri

You are welcome Aatmaj!

Collapse
 
yamancpu profile image
Yaman

Thanks

Collapse
 
alishgiri profile image
Alish Giri

You are welcome.

Collapse
 
jofisaes profile image
João Esperancinha

I didn't know about any of this. Great to know. This is very very helpful thank you! I usually only use console.log and I can see with your post what a bad idea actually is to only use that.

Collapse
 
alishgiri profile image
Alish Giri

Thanks for sharing.

Collapse
 
ridwanmithu profile image
Ridwan Ahmed

Very Helpful list. The article would have been complete if you shared the screen shot of output for console.group and console.time

Collapse
 
alishgiri profile image
Alish Giri

Thank you. I have added screenshot of them.

Collapse
 
lyzab profile image
Elizabeth Ajileye

These are very useful. They would make solving algorithms with JavaScript easier. Thank you so much @alishgiri

Collapse
 
alishgiri profile image
Alish Giri

You are welcome! Elizabeth

Collapse
 
yashkashyap profile image
Yash Kashyap

These are the Well Good! But If you are working on server-side development or web dev development you can not use to debug you line of code.

Collapse
 
alishgiri profile image
Alish Giri

Yes, when it comes to backend I prefer using JSON.stringify(your_data, null, 2) and use console.log() on that converted data.

Collapse
 
rifatparadoxical profile image
RIFAT PARADOXICAL

What is group(), time() and time end() ?

Collapse
 
alishgiri profile image
Alish Giri

group(): It will create different level of logs. It can be used when you are logging different section of the app and want to mark them separately by grouping them together.

time() & timeEnd(): It starts a timer to track how long an operation takes.

If you try the examples on a project that you are working on then it will be easier to understand.

Collapse
 
liberty_nacho profile image
Leonardo Holanda e Silva

Awesome content