A smaller way to log variables in JavaScript
A familiar debugging habit in JavaScript is to log a variable so the DevTools output shows both its name and its value. The common form has been to print a label alongside the value:
console.log("myVariable: ", myVariable);
A shorter alternative that produces equivalent output in DevTools is to pass the variable inside an object literal:
console.log({ myVariable });
console.log({ myVariable });
Everything visible in the DevTools panel is still there:

The display style is a DevTools behavior, but the syntax is plain JavaScript: curly brackets create an object, and an object does not need to be assigned to anything to exist.
{
foo: "bar"
}
The same pattern appears when passing an object into a function, as in myFunction({ config: options });. Object creation also allows a shorthand in which only a variable is supplied, and the property name is implied:
const fruit = "apple";
// Shorthand
let x = {
fruit
}
console.log(x);
// Normal way, literally the same exact thing
x = {
"fruit": fruit
}
console.log(x); // identical
So console.log({ myVariable }); relies on that shorthand and is logged as the object it becomes.
One downside: DevTools sometimes renders the result as a collapsed ▶ Object that must be clicked to reveal the value, and the heuristics behind that choice are unclear. For readers who dislike the collapsed view or object formatting generally, console.table({ myVariable }); is an alternative:




