JavaScript Array unshift() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Add at start

What You’ll Learn

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

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.

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.

📝 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.

⚡ Quick Reference

GoalCode
Add to startarr.unshift(value)
Add to endarr.push(value)
Remove from startarr.shift()
Return valueNew length (number)
Prepend without mutate[...items, ...arr]
Mutates array?Yes

📋 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

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Add one or more elements at index 0.

Example 1 — Add Elements to the Beginning

Prepend 1 and 2 to an existing number array.

JavaScript
const numbers = [3, 4, 5];

numbers.unshift(1, 2);

console.log(numbers);
// [1, 2, 3, 4, 5]
Try It Yourself

How It Works

Arguments insert left-to-right at the front. Original 3, 4, 5 shift to indexes 2, 3, 4.

Example 2 — Return Value Is New Length

unshift() returns a number, not the array.

JavaScript
const items = ["b", "c"];

const newLength = items.unshift("a");

console.log(newLength);
// 3

console.log(items);
// ["a", "b", "c"]
Try It Yourself

How It Works

Same return contract as push(). Use items directly if you need the updated array.

📈 Practical Patterns

Compare ends, prepend UI lists, immutable alternative.

Example 3 — unshift() vs push()

Same values, different end of the array.

JavaScript
const start = [2, 3];
const end = [2, 3];

start.unshift(1);
end.push(1);

console.log("unshift:", start);
// [1, 2, 3]

console.log("push:", end);
// [2, 3, 1]
Try It Yourself

How It Works

unshift targets index 0. push appends after the last element.

Example 4 — Prepend a New Notification

Show newest item first in a feed-style list.

JavaScript
const notifications = ["Older alert", "Previous message"];

notifications.unshift("New message just arrived");

console.log(notifications[0]);
// "New message just arrived"

console.log(notifications);
// ["New message just arrived", "Older alert", "Previous message"]
Try It Yourself

How It Works

UI lists that show latest-first often prepend with unshift or build a new array with spread in React state.

Example 5 — Non-Mutating Prepend with Spread

Keep the original array unchanged.

JavaScript
const original = [2, 3, 4];

const updated = [1, ...original];

console.log(updated);
// [1, 2, 3, 4]

console.log(original);
// [2, 3, 4]  ← unchanged
Try It Yourself

How It Works

In React or Redux, prefer creating a new array instead of mutating with unshift.

🚀 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 prependarr.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.

📝 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.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.unshift() Excellent

Bottom line: Safe in any JavaScript environment. Watch performance on very large arrays.

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.

💡 Best Practices

✅ Do

  • Pass multiple items in one unshift call
  • Use for prepend / newest-first UI patterns
  • Remember return value is length
  • Use spread in immutable state updates
  • Use push + shift for FIFO queues

❌ Don’t

  • Expect unshift to return the array
  • Call unshift in a tight loop when one call works
  • Use unshift + pop for FIFO queue semantics
  • Mutate shared arrays without copying in React
  • Confuse unshift with push

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.unshift()

Add elements at the beginning of an array.

5
Core concepts
02

Start

Index 0.

Position
🔢 03

Returns

New length.

Number
🔄 04

Mutates

In place.

Side effect
05

vs push

Front vs back.

Pair

❓ Frequently Asked Questions

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.

Continue to values()

Learn how to get an iterator over array elements with values().

values() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful