r/learnjavascript • u/denvercoder1 helpful • Sep 07 '20
When logging several variables, you can wrap them in curly braces to better see which value belongs to which variable
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
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
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
2
4
4
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
1
1
1
1
1
1
-3
2
u/denvercoder1 helpful Sep 07 '20
Gif credit to @sulco on Twitter