Handbooks / JavaScript / Chapter 4
Modern JavaScript Features
7 pages · ~5 min✓ Reviewed
Builds on this and Prototypes. Next up: Arrays and Objects in Practice.
Modern JS Mental Model
1. Unpack, spread, collect
- Arrays unpack by position
- Objects unpack by key
...is spread or rest, by where it sits
javascript
const [a, b] = arr; const { x, y } = obj; const copy = [...arr, 5]; const [first, ...others] = arr;
| Spread | Rest | |
|---|---|---|
| Direction | expands out | collects in |
| Takes | iterable or object | remaining items |
| Gives | separate elements | one array or object |
2. Safe access
?.short-circuits to undefined when the left is null or undefined- Without it: TypeError
??returns the right side only for null or undefined
javascript
const city = user?.address?.city ?? 'N/A';
undefined from ?. falls through to ??
3. Template literals
- Backticks:
Hi ${name} ${}embeds any expression- Strings can span multiple lines
Remember
count ?? 10 keeps 0; only null and undefined fall back.
Destructuring, Spread and Rest in Practice
1. Rename, Default, Nest
{ id: userId }renames the key touserIdrole = 'guest'applies only when the value is undefinednull,0and''keep their value, no default- Nested
{ address: { city } }throws ifaddressis undefined
javascript
const { id: userId, role = 'guest' } = user; const { address: { city } } = user;
2. Rest and Spread
| Rest | Spread | |
|---|---|---|
| Does | collects the remaining |
Optional Chaining, ?? and Template Literals
1. Safe access and ??
obj?.prop,obj?.[key],fn?.(args)- Broken link gives undefined, no throw
- Guards only the part left of
?.:a?.b.cthrows ifbis undefined
| || | ?? | |
|---|---|---|
| Replaces | every falsy value (0, '', false) | only null, undefined |
count = 0 | gives 10 | keeps 0 |
Array Methods: map, filter, reduce
1. What each one does
| Method | Returns | Callback |
|---|---|---|
map | new array, same length | transforms each item |
filter | new array, maybe shorter | keeps items when truthy |
reduce | one value | (acc, item) => next, initial |
- Always pass the initial value to
reduce [].reduce(fn)with no initial throws TypeError
ES Modules and Map and Set
1. Exports and Imports
export const f = 1pairs withimport { f } from './m.js': exact name in braces- Default:
import any from './m.js', any name; one default per module - Imports are live read-only bindings, not copies
- Modules run once, are cached, and are strict mode
2. Loading Rules
javascript
const { f, default: g } = await import('./m.js');
import() returns a promise; await gives the module namespace object
- Static
importonly at the top level - Node:
.mjsfile or"type": "module" - Include the file extension:
'./m.js'
Modern JS Cheat Sheet
1. Unpack and Combine
- Rest goes last; defaults apply only to
undefined - Spread is a shallow copy; later keys win
javascript
const { a, b = 1, ...rest } = obj const [x, , z] = arr const m = { ...a, ...b } const all = [...a, ...b] fn(...args)
2. Safe Access and Pipeline
javascript
user?.address?.city ?? 'N/A'
arr.filter(f).map(m).reduce(r, init)??replaces only null/undefined;||any falsy- reduce: pass init, return acc
3. Modules and Collections
Check yourself
What does this print?
10 0||replaces any falsy value,??only null or undefined
const count = 0; console.log(count || 10, count ?? 10);
What does ['1','2','3'].map(parseInt) return, and how do you fix it?
[1, NaN, NaN]: map passes the index as parseInt's radix- Fix:
.map(Number)
With const user = {}, what does user?.address.city do?
- Throws a TypeError:
?.guards onlyuser, andaddressis undefined - Write
user.address?.city