Modern Syntax
Destructuring, spread/rest, optional chaining, nullish coalescing, and template literals syntax you'll see in nearly every modern JS/React codebase.
Array destructuring
Pulls values out of an array into individual variables, by position:
Object destructuring
Pulls values out of an object into variables, by key:
Destructuring shows up constantly in function parameters, especially in React:
Spread operator (...)
Expands an iterable (array, string, object) into individual elements. Common uses:
Note: spread only performs a shallow copy nested objects/arrays are still shared by reference.
Rest parameters
The mirror image of spread: collects the remaining elements into a single array or object.
Optional chaining (?.)
Safely accesses deeply nested properties without throwing if something along the way is null/undefined. If any link in the chain is nullish, the whole expression short-circuits to undefined instead of throwing.
Without ?., the above would need something like user && user.address && user.address.city.
Nullish coalescing (??)
Returns the right-hand value only when the left-hand value is null or undefined unlike ||, it does not trigger on other falsy values like 0, "", or false.
?. and ?? are frequently combined:
Template literals
Backtick-delimited strings that support embedded expressions and multi-line text:
Tagged templates
A function can process a template literal before it's turned into a string used by libraries like styled-components, and for things like automatic escaping: