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
 
john_poku_4059fc2b8f98fb5 profile image
John Poku

Great

Collapse
 
harry625 profile image
Muhammad Ateeq

That's a neat debugging technique! Using console.trace() within console.group() sounds really efficient for organizing and accessing detailed trace information without cluttering the console. Thanks for sharing this tip—it's definitely going to be useful in my debugging toolkit!

Collapse
 
john_poku_4059fc2b8f98fb5 profile image
John Poku

Nice

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

There are many like

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

thanks for sharing

Collapse
 
modelair profile image
modelair

the article of 2024. 🤦‍♂️

Collapse
 
thaisavieira profile image
Thaísa Vieira

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

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
 
harry625 profile image
Muhammad Ateeq

Thanks for pointing that out! It's interesting to see how console.info() has become so similar to console.log() across different browsers. Using emojis to differentiate log messages is a great idea—adds a bit of fun and clarity to debugging!

Collapse
 
ninhnd profile image
Dang Ninh

the JSON.stringify life pro tip could also be applied to the browser environment when you're trying to log out something that has lost its reference due to garbage collection (for example, you try to log some value of an object before a page unload)

Collapse
 
alishgiri profile image
Alish Giri

Thanks for mentioning!

Collapse
 
harry625 profile image
Muhammad Ateeq

Great points! While console.log is a go-to for many developers, exploring alternatives can significantly enhance productivity. Using console.dir() for detailed hierarchical listings and JSON.stringify(your_data, null, 2) for formatted logs in the terminal are excellent practices. These methods improve readability and help prevent the clutter often associated with complex objects. Thanks for sharing these tips!

Collapse
 
retrop5 profile image
retrop5

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

Collapse
 
samkeen profile image
Sam Keen

thanks for the post. So many console.'s I didn't know about. I really like console.group() to make the logs more readable.

Collapse
 
praneshchow profile image
Pranesh Chowdhury

Wow, that's cool. I saved it.

Collapse
 
10102004tan profile image
Nguyen Phuong Tan

great list!

Collapse
 
girordo profile image
Tarcísio Giroldo

Really useful!

Collapse
 
forever585 profile image
Forever585

Thank you for your useful tips

Collapse
 
bvnkame profile image
Bvnkame

Thanks so much! <3

Collapse
 
sws-5007 profile image
Harry Potter

Great Article!
Thanks so much for your posting.
*-)

Collapse
 
essijunior profile image
NDANG ESSI Pierre Junior

I really like console.table()

Collapse
 
uniquesolution29 profile image
Jason

I like this post!

Collapse
 
devoskar profile image
Oskar Pietrucha

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

Collapse
 
harry625 profile image
Muhammad Ateeq

That's a smart approach! Using breakpoints in DevTools is a great way to inspect elements and understand their behavior step by step. It allows for precise debugging and ensures a thorough understanding of the code's execution. Happy debugging!

Collapse
 
john_poku_4059fc2b8f98fb5 profile image
John Poku

GGreat

Collapse
 
alishgiri profile image
Alish Giri

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

Collapse
 
john_poku_4059fc2b8f98fb5 profile image
John Poku

Nice

Collapse
 
john_poku_4059fc2b8f98fb5 profile image
John Poku

Nice

Collapse
 
ice_ profile image
Ayz Abdulwahid

this is very helpful, thank you!

Collapse
 
mdrejon profile image
Sydur Rahman

Really interesting

Collapse
 
uzeyir-yariz profile image
uzeyir-yariz

very nice, a loot thanks

Collapse
 
alishgiri profile image
Alish Giri

You are welcome!

Collapse
 
nafiz_iqbal_f2dbf653107b3 profile image
Nafiz Iqbal

Console.log("GEM")

Collapse
 
rennyson10oa profile image
Rennyson100a

I only knew the console.log and used it for everything, good article, you open my eyes :)

Collapse
 
zyrain profile image
Mads Jensen

Awesome.. ! :)

Collapse
 
asachanfbd profile image
Abhishek Sachan

This is so cool!

Collapse
 
gadrawingz profile image
Gad Iradufasha

Insightful to read!

Collapse
 
thatcoolguy profile image
dami

Thanks for this

Collapse
 
litlyx profile image
Antonio | CEO at Litlyx.com

This so cool!

Collapse
 
kn0w1dg3eb profile image
Eric Boudreau

Thank you!

Collapse
 
raljowair profile image
Rayan Juwair • Edited

WOW, I didn't know about that at all.

Collapse
 
luckyx128 profile image
Lucas de Amorim

Great content, I will definitely use it during my work

Collapse
 
arnar_freyrkristinsson_4 profile image
Arnar Freyr Kristinsson

Wow, definetly going to make use of that!

Collapse
 
gospel_asibu_97b760bce60a profile image
Gospel Asibu

Thanks

Collapse
 
stephen_adeoye_3cd2ad0f66 profile image
Stephen Adeoye

Great 👌,I don't use console.table before

Collapse
 
molla_yabir_e713a76e017ec profile image
Molla Yabir

I love this great

Collapse
 
pratham_varma_7fe4b9a5da8 profile image
Pratham Varma

Thank you!

Collapse
 
michael_addo_f14f7f3b3915 profile image
Michael Addo

I always eat and drink with console.log() when debugging.. Thanks for the tip

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
 
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
 
jasgiigli profile image
JasGiigli

As seasoned web developers, relying solely on console.log() limits our ability to gain comprehensive insights during debugging. Thankfully, there are more sophisticated logging techniques and tools that enable us to go beyond basic logs, offering enhanced error tracking and performance monitoring to improve overall development efficiency

Collapse
 
alberto_tonet_9aee523a56d profile image
Alberto Tonet • Edited

Thanks for this hint!
I think that trying these options within the Turbo Console Log vscode extension can be an interesting combo too!

Collapse
 
alishgiri profile image
Alish Giri

Nice. Thanks for sharing.

Collapse
 
abhinavnigam2207 profile image
Abhinav Nigam

A few more have been covered in the below article:
medium.com/@abhinavnigam2207/conso...

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
 
koas profile image
Koas

Nice list! Also, recently a port of the Console API was developed for PHP, quite interesting!

Collapse
 
alishgiri profile image
Alish Giri

nice!

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
 
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
 
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
 
sfskysf profile image
Sky

Good to know about console.clear i had no idea i could control that

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
 
john_poku_4059fc2b8f98fb5 profile image
John Poku

Great

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
 
aatmaj profile image
Aatmaj

Thanks! ❤️

Collapse
 
alishgiri profile image
Alish Giri

You are welcome Aatmaj!

Collapse
 
relmasha profile image
RElmasha

Thank you !👍

Collapse
 
fazlyalahiru profile image
Fazly

Great. But I use “Console Ninja” to see my console on the IDE on the go.

Collapse
 
alishgiri profile image
Alish Giri

Nice!

Collapse
 
faisal_alibutt_57a204227 profile image
Faisal Ali butt

console.dir(["apples", "oranges", "bananas"]);
Is terminator important?

Collapse
 
alishgiri profile image
Alish Giri

Yes, it is.

Collapse
 
faisal_alibutt_57a204227 profile image
Faisal Ali butt

I am new one that's why I am asking

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
 
mary_oppong_0a95437f8d99e profile image
Mary Oppong

👍 Great

Collapse
 
vinay_kori_9deed631d16973 profile image
vinay kori

its useful..

Collapse
 
chandra_pantachhetri profile image
Chandra Panta Chhetri

Quick and straight to the point, I like it! I'll be using console.group() from now to keep the console more organized :)

Collapse
 
alishgiri profile image
Alish Giri

Glad it helped!

Collapse
 
udinfm profile image
Very Shafrudin

Very informative

Collapse
 
zionmosesh profile image
Zion Saint Foundation

This is helpful

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
 
godwin_ashiekaa_7090ba441 profile image
Godwin Ashiekaa

Thanks. You've just expanded my knowledge on console.log()

Collapse
 
alishgiri profile image
Alish Giri

You are welcome.

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
 
kaush-24k profile image
kaushik venkatanagasai

good info

Collapse
 
abijith_dhayananda profile image
Abijith Dhayananda

Idk if I am right? Is it to make the website least vulnerable?

Collapse
 
alishgiri profile image
Alish Giri

Actually, these are just to make development easy and has nothing to do with website vulnerability.

Collapse
 
haddy profile image
Hadassah Banda

Thank you!

Collapse
 
md_arifulislam_47b9b3e84 profile image
MD ARIFUL ISLAM

helpful

Collapse
 
kenn_galvez_b29933b41df11 profile image
Kenn Galvez

This is indeed useful if you are trying to debug in the consoles and etc. Great!

Collapse
 
liberty_nacho profile image
Leonardo Holanda e Silva

Awesome content