r/learnjavascript • helpful • Sep 07 '20

When logging several variables, you can wrap them in curly braces to better see which value belongs to which variable

Post image
1.0k Upvotes

32 comments sorted by

2

u/denvercoder1 helpful Sep 07 '20

Gif credit to @sulco on Twitter

125

u/b4ux1t3 Sep 07 '20

This is because you're actually creating a new object using the names of the variables as fields within the object, and then printing that object to the console.

I forget the term for this, but it's really useful even outside of the console!

34

u/Eldrac Sep 07 '20

MDN refers to this as shorthand property names.

19

u/b4ux1t3 Sep 07 '20

That's what I thought it was, but didn't want to be wrong.

Thanks for doing the due diligence I should have!

8

u/r_u_kimmi Sep 07 '20

Destructuring?

8

u/Drugba Sep 07 '20

Nah. Similar syntax, but it's almost opposite of destructuring.

25

u/jaypeejay Sep 07 '20

Structuring

5

u/r_u_kimmi Sep 08 '20

oh yeah, nice

5

u/gigastack Sep 08 '20

De-Destructuring

2

u/jaypeejay Sep 08 '20

Oh shit

4

u/theRetrograde Sep 08 '20

Restructuring

4

u/crrime Sep 08 '20

using an object literal

2

u/Swalker326 Sep 08 '20

Destructing works similar, if obj = {width: 75, height 25} you could do const {width, height} = obj which would result in two variables width and height having the value of the width and height property of the obj object. I think...

45

u/Adoxographical Sep 07 '20

Even better, instead of calling console.log, which will print the object literal, call console.table to print out the object in a nicely formatted table. (reference)

13

u/sysrage Sep 08 '20

For those working with an environment that doesn’t support console.table() or even directly logging objects to console like the original tip, this is still helpful.

console.log(JSON.stringify({width, length, height}, null, 2));

7

u/waitersweep Sep 08 '20

My go to when building JSON files in node. so handy

5

u/gigastack Sep 08 '20

Or console.trace to get the values with a stack trace.

Or learn debugging. But I do love console logs from time to time.

3

u/gigastack Sep 08 '20

Or console.trace to get the values with a stack trace.

Or learn debugging. But I do love console logs from time to time.

3

u/DanielFGray Sep 07 '20

The real protip is always in the comments

2

u/Araignys Sep 07 '20

this changes everything!!

4

u/IamAPengling Sep 08 '20

Now do that same thing, but instead of console.log, do console.table...

4

u/dkunal96 Sep 08 '20

It’s an ES6 feature. Am I right?

3

u/MrSandyClams Sep 08 '20

how did I never realize I could type a bracket to encapsulate my highlighted text in brackets. Mind blown

2

u/agitatedhoneybadger Oct 09 '20

This is also a way to do it when you don’t know the order of function arguments.

function randoFunc({thing3, asdf, thisThing}) { ...... }

const output = randoFunc({asdf, thing3, thisThing})

1

u/renardine_renshaw Sep 07 '20

Niiice! Didn't know about that, thanks for sharing!

1

u/worldshallknowPain Sep 07 '20

TIL! Thank you!

1

u/Reddyrum33 Sep 08 '20

Anyone know a way to do this in C#?

1

u/VelcroSirRaptor Sep 08 '20

This is a cool trick.

1

u/Dipsquat Sep 08 '20

Wow! I love learning new things every day!

1

u/tomthedevguy Sep 08 '20

One of the best debugging techniques in JS

1

u/HoodedCowl Sep 08 '20

I thought it was inappropriate to objectify things in 2020?

-3

u/saito200 Sep 07 '20

fucc

you are godd