Array.unshift() Prepend items
Last updated: 2026-09-09
Array.unshift() Prepend items
unshift prepends elements and returns the new length. O(n) — every element shifts right.
| Category | Arrays |
|---|---|
| ES version | ES1 (1997) |
📝 Syntax
const newLength = array.unshift(element1, ..., elementN);
⚙️ Parameters
| elementN | Elements prepended in the given order. |
|---|
Returns:The new length.
Mutates the original:Yes (in place)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
3
new msgA
newer
⚠️ Warning unshift is O(n) — every element shifts right.
💡 Tip unshift(...arr2) prepends another array's items via spread.
🌐 Browser support
| Browser | |||||
|---|---|---|---|---|---|
| Array.unshift() | ✓ v1 | ✓ v12 | ✓ v1.5 | ✓ v3 | ✓ v10.5 |
| Supported by all modern browsers | |||||
🏷️ Related Array Methods
💡 Related Cases
More case studies coming soon — check back later.
📚 Related Tutorials
❓ FAQ
Qunshift vs push?
Aunshift prepends (O(n)); push appends (O(1)) — prefer push unless order matters.
QWhat does unshift return?
AThe new length, like push.
QHow to prepend an array's items?
Aarr.unshift(...arr2) spreads; unshift(arr2) would nest the whole array.