DEV Community

Stop using default console.log, check this instead

Guilherme Toti on May 21, 2020

So, you are always using console.log to debug your application, right? In this post, I will show you alternatives to better debugging. Int...
Collapse
 
guilhermetoti profile image
Guilherme Toti

Thanks man!
The % operator is like a placeholder.
Let’s say you have:

console.log(“my %s”, “string”)
> “my string”

In this case, the second argument (“string”) will be replaced at %s.

There is also a %f for float numbers, and it’s cool because you can do something like:

console.log(“my %.2f”, 12.3456789)
> 12.34
Collapse
 
guilhermetoti profile image
Guilherme Toti

You can do it the same way! You can use ... assignment and array of values in a console.log!

Just like:

const myArr = [1,2,3]
console.log(...myArr)
console.log(myArra)

Both should work perfectly!

Collapse
 
rubenwap profile image
Ruben Sanchez

In the first example, my preferred method is normally the template string. I have trouble seeing the advantage of the % operator compared to concatenating the variables. Other than that, this article was useful and discovered me the dir and group methods!

Collapse
 
guilhermetoti profile image
Guilherme Toti

Yeah, I do not see a lot of advantages using % operator either, but I bring it just as informative content, it's a "good to know" thing...
Thanks! yesss, dir an group can be very helpful!

Collapse
 
willydee profile image
Willy Schott

The % syntax is for seasoned programmers who started with C and are now bald and wrinkly. printf still ringing a bell? 👩🏼‍🦲

Thread Thread
 
guilhermetoti profile image
Guilherme Toti

ahahaha

Collapse
 
robrecord profile image
Record

Great, thank you for this!

In your first example I think it is supposed to read
console.log('Hello ' + a);

Collapse
 
guilhermetoti profile image
Guilherme Toti

Thank you!
Yes! You're right! That was my mistake! Just fixed it, thanks!!!

Collapse
 
salyadav profile image
Saloni Yadav

Mind blowing. Thank you for this article. Pity I always did console.log to dissect my objects and arrays. I think i going to overuse console.table now. :)

Collapse
 
guilhermetoti profile image
Guilherme Toti

You’re welcome!
It is really mind blowing! I have been using .log for years! Now I can debug stuff way faster!

Collapse
 
maciejcieslik profile image
Maciej Cieslik

Thanks for the article, console.trace() can be usefull sometimes.

Collapse
 
guilhermetoti profile image
Guilherme Toti

Thanks!
True! I completely forgot about console.trace(), thanks for remind me!

Collapse
 
salyadav profile image
Saloni Yadav

What does trace do?

Thread Thread
 
guilhermetoti profile image
Guilherme Toti

Trace will show the stack trace, where that fails.
Let’s say in your app you have like a function that calls another function, that calls another function.
If you add a trace in the last function and something happen, it will log exactly where happened, like:

function_3 at line 1 (example)
function_2 at line 5 (example)
function_1 at line 88 (example)
Collapse
 
namstel profile image
Namstel

Oo! I like that one a lot. :)

Collapse
 
foksa profile image
Djordje Radakovic

First example can also be written as console.log('Hello', a)

Collapse
 
randalvance profile image
Randal Vance Cunanan

I few ones I didn't know like console.dir. Thanks for this!

Collapse
 
guilhermetoti profile image
Guilherme Toti

You're welcome!

Collapse
 
sidwarkd profile image
Kevin Sidwar

Sorry for the dumb question but do you need a certain console for this to work? I tried both the CSS and grouping things after updating to the latest version of node and neither works. What am I missing? I'm on a Mac using the Terminal app.

Collapse
 
guilhermetoti profile image
Guilherme Toti

Oh, I’m not sure how it works on node, if node has some limitations, but, my examples were all done by any web browser console (developer tools)

Collapse
 
sidwarkd profile image
Kevin Sidwar

Oooooooooh! Haha. I totally assumed the node terminal. That makes a LOT more sense now. Thanks for the fast reply.

Thread Thread
 
guilhermetoti profile image
Guilherme Toti

ahahaha! you're welcome!

Collapse
 
pvcodes profile image
Pranjal Verma

Hey guys, I have been working on my very first APIs project and in that project I'm making a "QUIZ APP", where from an API. I have been taking 10 question and giving them multiple choices and at end they will be getting scored. I'm using NODE.JS, EXPRESS, REQUEST, EJS for the app.

The problem comes here when i get a string from API. I change it to an object using JSON.parse() and sending it to EJS files and then displayed. but when their are symbols in question like ", ' , and more they are displayed as &139; , " like that.

                      HOW DO I FIX THAT.
Collapse
 
bdougherty profile image
Brad Dougherty

These days, I use this a lot: console.log({ a, b });

Collapse
 
guilhermetoti profile image
Guilherme Toti

That's awesome and very useful! I'm using that a lot though!

Collapse
 
yaireo profile image
Yair Even Or

I've authored a tiny utility to easier styled console logs:

github.com/yairEO/console-colors

Collapse
 
guilhermetoti profile image
Guilherme Toti

That’s cool!!

Collapse
 
emax83 profile image
Emanuele Mignano

Great and usefull article!! thanks!!
console.table open my eyes!!
i will use it when i receive json response and i need to view results.

Collapse
 
kendalmintcode profile image
Rob Kendal {{☕}}

Super useful, thanks for this Guilherme!

Collapse
 
guilhermetoti profile image
Guilherme Toti

You're very welcome! I'll probably bring more useful things here soon!

Collapse
 
darryl profile image
Darryl Young

Thanks for sharing, Guilherme.

Collapse
 
guilhermetoti profile image
Guilherme Toti

You’re welcome!

Collapse
 
jmvallejo profile image
Juan Manuel Vallejo

Useful info! Thanks :)

Collapse
 
guilhermetoti profile image
Guilherme Toti

You're welcome man!

Collapse
 
rafaelrferreira profile image
Rafael R. Ferreira 🕹️🐕

awesome tips!

Collapse
 
guilhermetoti profile image
Guilherme Toti

Thanks man!

Collapse
 
anandprem333 profile image
Anand Prem • Edited

console.time also worth to mention!
Apart from that nice and concise article

Collapse
 
guilhermetoti profile image
Guilherme Toti

That's true! console.time also worth the search! Good mention

Collapse
 
nicknds profile image
NIku Nitin

Thanks for the article.. Its really awesome..
I never knew console had these many methods.

Collapse
 
guilhermetoti profile image
Guilherme Toti

You’re very welcome!
Yep, console has so many things that can help us, developers!

Collapse
 
julianeon profile image
Julian Martinez

I never knew about console.table. Thanks for the great article!

Collapse
 
guilhermetoti profile image
Guilherme Toti

You're welcome man! Yep, some console mysteries were revealed 👀

Collapse
 
sarahb831 profile image
sarahb831

This was great, thank you! So nice to have options to console.log(), this will make it much easier to debug!!!

Collapse
 
guilhermetoti profile image
Guilherme Toti

You're welcome!

Collapse
 
rahuldkjain profile image
Rahul Jain

Loved it! Very informative