Hacker News new | past | comments | ask | show | jobs | submit login

Another useful trick I've been using for some time is

console.log({ a, b })

where a and b are some variables. This will print the name of the variable next to its value. So it can be thought as a faster version of

console.log('a: ' + a)




That's due to a shorthand added in ES2015 that lets you construct an object where keys are equal to the name of the variable. So that's short for

    console.log({ a: a, b: b })
and then the console just prints out the new object.


Nice trick.

What I do is console.log('MyVariable',MyVariable,'MyOtherVariable',MyOtherVariable);

When writing it I simply copy the variable, "paste twice" and then "wrap with quotation marks".

That one is pretty nice because you can add ,'\n' to have a new line. console.log('MyVariable',MyVariable,'\n','MyOtherVariable',MyOtherVariable);


Nice! Thank you


Completely tangential, but you can also do the opposite with nesting, which I learned about literally yesterday:

const { some: { nested: { field } } } = tree; // field === tree.some.nested.field

or rename things while you destructure them:

const { crappyFieldName: betterName } = myObject; // betterName === myObject.crappyFieldName

Now I can't stop using this feature...




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: