3 ms·
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
by nicusor 8y ago
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)
- foreigner 8y agoNice! Thank you
- anonytrary 8y agoCompletely 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...
- jayrhynas 8y agoThat'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.
- Raphmedia 8y agoNice 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);