unshift
unshift() adds one or more new elements at the start of an array and returns the new length. Use it when the newest item belongs on top: latest notifications, a pinned item, a "recently viewed" list.
Syntax
const newLength = array.unshift(item1, item2 /* , ... */);
item1, item2, ...: values to add at the front, in the order given.- Returns: the new
lengthof the array (not the array).
Mutates the original array: every existing element moves up one index. Non-mutating alternatives: spread,
[newItem, ...arr], orarr.toSpliced(0, 0, newItem).
1. Basic usage
const num = [1, 2, 4];
num.unshift(-12); // 4 (new length)
num; // [-12, 1, 2, 4]
num.unshift("a", "b");
num; // ["a", "b", -12, 1, 2, 4] (order is kept)
2. Real-world example: newest notification first (React)
const [notifications, setNotifications] = useState([]);
function addNotification(msg) {
setNotifications((prev) => [{ id: Date.now(), msg }, ...prev]); // immutable "unshift"
}
Calling prev.unshift(...) would mutate state, and React would not re-render because the array reference is unchanged.
3. Recently viewed (keep max 5)
function addRecent(list, item) {
return [item, ...list.filter((x) => x !== item)].slice(0, 5);
}
4. Performance
unshift is O(n): every existing element has to move up one index. push is O(1) (amortized). If you add to the front often, consider pushing and reading in reverse, or calling reverse() once at the end.
Gotchas
- Returns the new length, so
arr = arr.unshift(x)turnsarrinto a number. arr.unshift(1, 2)gives[1, 2, ...], butunshift(1)followed byunshift(2)gives[2, 1, ...].- Passing an array adds it as one nested element:
[3].unshift([1, 2])gives[[1, 2], 3]. Spread it instead:unshift(...items).
Interview questions
1. What does unshift return?
Answer
The new length of the array, not the array itself.
2. Why is unshift slower than push?
Answer
unshift has to move every existing element one index up (O(n)). push writes to the end without moving anything (O(1)).
3. How do you add to the start of a React state array?
Answer
Create a new array: setItems(prev => [newItem, ...prev]). Never call unshift on state.
4. What is the result of const a = [3]; a.unshift(1, 2); a.unshift(0);?
Answer
[0, 1, 2, 3]. The first call adds 1 and 2 in order, the second adds 0 in front.