The unshift() method adds one or more elements to the beginning of an array and shifts existing items right. It returns the new length. This tutorial covers syntax, comparison with push(), prepend patterns, and five hands-on examples.
01
Syntax
unshift(...items)
02
Add start
Index 0
03
Returns
New length
04
Mutates
In place
05
vs push
Start vs end
06
ES3
Universal
Fundamentals
Introduction
Need a new item at the front of a list? A fresh notification, a recent search term, or a priority task? unshift() inserts at index 0 and moves everything else one slot to the right.
It is the mirror of push() (which adds at the end). Like push(), it returns the new array length—not the array itself.
Concept
Understanding the unshift() Method
array.unshift(item1, item2, ...) inserts arguments at the start in order: the first argument becomes index 0, the second index 1, and so on. Existing elements shift right to make room.
On large arrays, unshift() can be slower than push() because every element must move. For frequent front inserts at scale, consider a deque structure or prepend with spread when immutability matters.
💡
Beginner Tip
For a classic FIFO queue (first in, first out), use push() + shift(). unshift() + pop() does not give FIFO order.
Foundation
📝 Syntax
General form of Array.prototype.unshift:
JavaScript
array.unshift(element1, element2, ..., elementN)
Parameters
element1, element2, ... — values to insert at the beginning.
Return value
The new length of the array (a number).
Common patterns
arr.unshift(item) — prepend one element.
arr.unshift(a, b, c) — prepend multiple in one call.
arr.unshift(...otherArr) — prepend all items from another array.
const next = [item, ...arr] — non-mutating prepend.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Add to start
arr.unshift(value)
Add to end
arr.push(value)
Remove from start
arr.shift()
Return value
New length (number)
Prepend without mutate
[...items, ...arr]
Mutates array?
Yes
Compare
📋 unshift() vs push() vs shift() vs spread
Front add vs end add vs front remove vs immutable prepend.
unshift
add first
Mutates
push
add last
Mutates
shift
remove first
Opposite
spread
new array
Safe
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.
In React or Redux, prefer creating a new array instead of mutating with unshift.
Applications
🚀 Common Use Cases
Notification feeds — newest item at index 0.
Recent searches — latest query at the top.
Undo history — prepend latest action (with length cap).
Priority insertion — urgent task before others.
Bulk prepend — arr.unshift(...items) in one call.
FIFO queue — use push + shift, not unshift + pop.
🧠 How unshift() Runs
1
Increase length
Make room for new elements at index 0.
Grow
2
Shift existing right
Each old element moves up one index.
Move
3
Insert new values
Place arguments at indexes 0, 1, …
Insert
4
✅
Return length
Array updated in place.
Important
📝 Notes
Mutates the original array.
Returns new length, not the array.
Multiple arguments insert in argument order at the front.
Slower than push on large arrays (all elements shift).
Prefer one call with many args over repeated unshift in a loop.
ES2023: no toUnshifted—use spread for immutable prepend.
ES3—universal browser support.
Compatibility
Browser & Runtime Support
Array.prototype.unshift() has been available since JavaScript 1.2 / ES3 and works in every browser and Node.js environment.
✓ Baseline · ES3
Array.prototype.unshift()
Supported everywhere: Chrome, Firefox, Safari, Edge, IE, and all Node.js versions. Behavior is consistent across platforms.
100%Universal support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.unshift()Excellent
Bottom line: Safe in any JavaScript environment. Watch performance on very large arrays.
Wrap Up
Conclusion
unshift() prepends one or more items at the start of an array and returns the new length. Pair it mentally with push() (end) and shift() (remove start). Use spread when you must not mutate the original.
Next, learn values() to iterate array elements as an iterator.
unshift() adds one or more elements to the beginning of an array, shifts existing elements to higher indexes, increases length, and returns the new length.
Yes. unshift() changes the original array in place. Use spread ([...items, ...arr]) or concat when you need a new array without mutating the original.
The new length of the array after insertion—not the array itself. This matches push() behavior.
unshift() adds at index 0 (the start). push() adds at the end. unshift() is slower on large arrays because every existing element moves one index right.
unshift() adds to the front. shift() removes from the front. They are opposites at the start of the array.
unshift() has been available since JavaScript 1.2 / ES3 and works in every browser and Node.js environment.
Did you know?
Calling unshift repeatedly in a loop re-shifts the entire array each time. Passing all items in one call—arr.unshift(a, b, c) or arr.unshift(...items)—is faster and clearer.