DEV Community

Allan N Jeremy
Allan N Jeremy

Posted on

Three types of console.log users. Which one are you?

Any js developer has definitely used console.log() before. Among these 3 main ways to print variables with text within console.log(), I am curious to see what kind of developers people are.

I have added comments for of the three methods mentioned feel free to vote for the method you use by liking the comment that corresponds to how you use `console.log(). I'd also love to hear why you use the method that you use.

Top comments (9)

Collapse
 
allanjeremy profile image
Allan N Jeremy • Edited

Method two: Template literals (ES6+)

By wrapping a string in backticks you can append a variable to the string by putting it inside ${}.

console.log (`my variable : ${myVariable}`);
Collapse
 
waylonwalker profile image
Waylon Walker

This is the way I should do it, 😜

Collapse
 
allanjeremy profile image
Allan N Jeremy • Edited

😁 either this or the other one, the other option does have its advantages (e.g. printing an object or other non-primitives without having to JSON.stringify it)

Collapse
 
allanjeremy profile image
Allan N Jeremy • Edited

Method three: separate arguments

console.log allows us to parse strings and variables as separate arguments

console.log ('my variable :', myVariable);
Collapse
 
waylonwalker profile image
Waylon Walker

KISS

Collapse
 
allanjeremy profile image
Allan N Jeremy

🤘🏾

Collapse
 
allanjeremy profile image
Allan N Jeremy

Method one: String concatenation

We can join strings using the + operator. As a result this allows us to append a variable to a string.

console.log ('my variable :'+ myVariable);
Collapse
 
gkhan205 profile image
Ghazi Khan • Edited

I just log it without any message and identify it with the filename and line number. 🙈

Collapse
 
allanjeremy profile image
Allan N Jeremy

Sounds like work 😅