April 2020
Intermediate to advanced
292 pages
6h 50m
English
Sometimes you put into an object a value contained in a variable. Suppose that you want to put a userId into a message’s metadata and you want to put it at the key userId. In days of yore, you would have to write { metadata: { userId: userId } }. That double userId is a wee bit redundant. It’s also repetitive.
The committee that maintains the JavaScript language spec thought so too, and so now, if you have a variable whose name is the same as the key you want in an object, you can use the shorthand of just putting they key name directly in the curly braces like so: { metadata: { userId } }.
And it doesn’t have to be all shorthand or no shorthand. The following is also valid:
| | const userId = uuid() |
Read now
Unlock full access