DEV Community

Vijay SRJ
Vijay SRJ

Posted on

Tips to save time writing console.log() in JavaScript

console.log() is of great help for debugging Javascript code .
But writing them is time consuming. You can save time by following these two tricks:

  1. Enclose variables in curly braces :

    console.log({input}) instead of console.log(“input”,input)

  2. Create a global variable and assign console.log() function to it . Use that variable instead of console.log

Demo for the above two :

https://fullstackdeveloper.guru/2020/11/12/how-to-save-time-typing-console-log-statements-in-javascript/

Top comments (9)

Collapse
 
danytulumidis profile image
Dany Tulumidis

nice tips. I find console.table very nice to display for example arrays in a very nice way!

Collapse
 
realabbas profile image
Ali Abbas

Yeah it's a good way to display

Collapse
 
codefinity profile image
Manav Misra

ℹ always 4get about that!

Collapse
 
vijaysrj profile image
Vijay SRJ

that is nice too!

Collapse
 
bugb profile image
bugb • Edited

We can do more with it:

  • Create a snipet for faster typing (see my gif)
  • Some log services like Cloudwatch will display console.log({input}) is not better than console.log(input)
  • For print object, we can use: console.dir({obj}, {depth: null})

Imgur

Collapse
 
chauchausoup profile image
Krishna Dhakal
console.log(console) 
Enter fullscreen mode Exit fullscreen mode

was an eye opening for me.

Collapse
 
vijaysrj profile image
Vijay SRJ

glad that it helped you :)

Collapse
 
bloodrave_n profile image
Pike Msonda

Neat tip. Love the first one more especially.

Collapse
 
vijaysrj profile image
Vijay SRJ

thank you :)