DEV Community

Charlie @ 90-10.dev
Charlie @ 90-10.dev

Posted on • Originally published at 90-10.dev on

Handy console.log tip: display variables names

We're yet to see a JavaScript developer that doesn't use console.log to perform a "quick debug". And it goes like this:

let theAnswer = 42
console.log(theAnswer);
Enter fullscreen mode Exit fullscreen mode

And the console will oblidge, showing:

42
Enter fullscreen mode Exit fullscreen mode

Encapsulate in an object

Here is a handy tip: encapsulate the variable in an object and the variable name will be displayed:

console.log( { theAnswer } );
Enter fullscreen mode Exit fullscreen mode

The output will now be:

{theAnswer: 42}
Enter fullscreen mode Exit fullscreen mode

Happy debugging!

Top comments (0)