Javascript

Javascript | Using object shorthand when debugging

Hello All,

I just found this on Internet and thought it would be nice and useful to all programmers. It helped me a lot when i debug things on my console.

When using console.log() in Node.js it can be hard to spot and understand variables you’re logging in your terminal, especially if they’re empty .

We now have a shorthand trick for it. Wow I am loving node.js now.

Use object shorthand so that every value has a name next to it e.g.

Example 1

var var1=23
var var2='hello world' 
console.log({ var1, var2 });

Output

{ var1 : 23, var2: 'hello world' }

Example 2

var obj={ name : 'vipin', company: 'xyz', location:'india'}

output

{ obj : { name : 'vipin', company: 'xyz', location:'india'} }

I had this difficulty to understand what is getting printed on my terminal. But now i have this hack or trick.

What is Javascript | Using object shorthand when debugging

JavaScript Object literal shorthand syntax is a bit unique in that it’s not so much a shiny new tool, but an improved syntax. Now it might be tempting to dismiss this kind of feature as less-than-stellar, but it’s worth working into your routine for a number of reasons. For example, an improved syntax can lead to less code, and not only that, the code you write can be easier to understand.

Hope thats useful. Please try and let me know.

Happy programming 🙂

A copy of the same article can be found at my medium post.

Leave a Reply

Your email address will not be published. Required fields are marked *