DEV Community

José M. Gilgado
José M. Gilgado

Posted on • Originally published at josemdev.com

1

Better Console Debug in Javascript

When debugging an object in Javascript, you usually do something like:

console.log(myObject);
Enter fullscreen mode Exit fullscreen mode

Or even:

console.debug(myObject);
Enter fullscreen mode Exit fullscreen mode

If we create the object with:

myObject = {
    name: 'test',
    size: 1234,
}
Enter fullscreen mode Exit fullscreen mode

And you do a console.debug from somewhere in the code, in the console you'll see:

So it might not be clear which object you're dealing with. To solve this you can do:

console.debug({myObject});
Enter fullscreen mode Exit fullscreen mode

The result is the name of the object and the full object printed:

This comes from ES6, it's usually called shorthand property names and it works because the key and the object variable has the same name.

It's the same thing that saying:

console.debug({myObject: myObject});
Enter fullscreen mode Exit fullscreen mode

But more convenient as you can see. 😉

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay