DEV Community

Cover image for Say no to console.log!
Alish Giri
Alish Giri

Posted on • Edited on

Say no to console.log!

Do you always use console.log in your projects during development?

And even though we will keep using console.log, there are other alternatives that will make your development more fun and productive.

 

NOTE: If you are viewing log in a terminal especially if you are a backend developer then you can try JSON.stringify(your_data, null, 2) and use console.log() on the result. This will make sure that you don't get {... value: [Object, Object]} in the log and the log will also be formatted making it easier to read.

 

console.dir()

For hierarchical listing of arrays and objects.

console.dir(["apples", "oranges", "bananas"]);
Enter fullscreen mode Exit fullscreen mode

console.dir example

 

console.table()

For rows and columns listing of arrays (might not be suitable for objects).

console.table(["apples", "oranges", "bananas"]);
Enter fullscreen mode Exit fullscreen mode

console.table array example

console.table({"a": 1, "b": 2, "c": 3});
Enter fullscreen mode Exit fullscreen mode

console.table object example

 

console.group()

console.log('This is the top outer level');

console.group('Task 1');
console.log('Task activity 1');
console.log('Task activity 2');
console.groupEnd();

console.group('Task 2');
console.log('Task activity 3');
console.log('Task activity 4');
console.groupEnd();

console.log('Back to the top outer level');

Enter fullscreen mode Exit fullscreen mode

console.group example

 

console.time() & console.timeEnd()

try {
  console.time("record-1");
  await someAsyncTask();
} catch (error) {
   // handle error
} finally {
  console.timeEnd("record-1");
}
Enter fullscreen mode Exit fullscreen mode

console.time log

 

console.clear()

This will clear the console.

 
I hope this was helpful! 🚀

Top comments (115)

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

Great

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
 
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
 
thaisavieira profile image
Thaísa Vieira

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

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
 
10102004tan profile image
Nguyen Phuong Tan

great list!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.