Array Destructuring
Array Destructuring
π¨βπΌ Excellent! You've mastered array destructuring.
π¦ Key patterns:
- Position-based extraction:
const [first, second] = arr - Rest pattern:
const [head, ...tail] = arr - Function return destructuring:
const [min, max] = getMinMax(nums)
This pattern is foundational for React hooks:
// useState returns an array you can destructure (also called a "Tuple")
const [count, setCount] = useState(0)
// useReducer too
const [state, dispatch] = useReducer(reducer, initialState)
The rest pattern (
...) is also the foundation for the spread operator, which
you'll use extensively for immutable updates.