Spread and Rest
Explain the spread (`...`) and rest syntax for copying and collecting values.
Short explanation
The spread operator (...) expands iterable values while rest parameters
collect function arguments or remaining array elements.
Syntax example
const arr = [1, 2, 3];
const arr2 = [...arr, 4];
function sum(...nums) {
return nums.reduce((a, b) => a + b, 0);
}
How JS handles it internally
Spread creates shallow copies for arrays and handles iterable unpacking. Rest parameters collect arguments into an array at call time.
FAQ
Q: Does spread perform deep cloning?
A: No β spread does a shallow copy. For deep cloning use structuredClone or a library.