November 28, 2020

Nick

Log variable name and value: the short way

There is a comfortable short way to log a variable name and value in JS

JavaScript

Log variable name and value: the short way

I found a very interesting article on css-tricks.com to log a variable in JS console.

// my old-fashioned way
console.log("myVariable: ", myVariable);

// the much cooler way 😎
console.log({ myVariable });

This works because the shorthand assignment for objects expects the variable name is equal to the object key. In JS the example is interpreted as:

{ 
  "myVariable": myVariable
}

Nick

I am a software developer who spends most of his spare time working on open source projects. I also like taking pictures and playing table football.