JavaScript Array shift() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Mutates

What You’ll Learn

The shift() method removes the first element, shifts the rest down, and returns the removed value. This tutorial covers syntax, five examples, FIFO queues with push(), comparison with pop(), and why shifting large arrays can be slower than end removal.

01

Syntax

arr.shift()

02

First out

Index 0

03

Returns

Removed item

04

Empty

undefined

05

Queue

FIFO + push

06

ES3

Universal

Introduction

pop() removes from the end; shift() removes from the start. Every remaining element moves down one index, so the array stays dense with no gap at the front.

Pair push (add to end) with shift (remove from start) to model a first-in-first-out (FIFO) queue—like a line where the first person in is the first person served.

Understanding the shift() Method

array.shift() deletes the element at index 0, moves index 1 to 0, 2 to 1, and so on, decrements length, and returns the removed first element.

On an empty array, shift() returns undefined without throwing. Like pop(), it mutates the array all references point to.

💡
Beginner Tip

Removing from the start re-indexes every other element. For huge arrays with frequent front removals, consider a dedicated queue structure—but for typical tutorial-sized lists, shift() is fine.

📝 Syntax

General form of Array.prototype.shift:

JavaScript
array.shift()

Parameters

  • None.

Return value

  • The removed first element.
  • undefined if the array is empty.

Side effects

  • Mutates the array: first element removed, others shift down, length decreases by 1.

Common patterns

  • const first = arr.shift() — remove and capture.
  • queue.push(item); queue.shift() — FIFO dequeue.
  • while (arr.length) arr.shift() — drain from front.
  • if (arr.length) arr.shift() — guard empty arrays.

⚡ Quick Reference

GoalCode
Remove first elementarr.shift()
Return valueRemoved element or undefined
Remove last elementarr.pop()
Add to frontarr.unshift(x)
Mutates array?Yes

📋 shift() vs pop() vs unshift() vs slice(1)

Choose start vs end removal and whether you need to mutate.

shift
remove first

Mutates

pop
remove last

Faster at scale

unshift
add first

Front insert

slice(1)
copy rest

No mutation

Examples Gallery

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

📚 Getting Started

Remove the first item and inspect the array.

Example 1 — Remove the First Element

Shift "red" off a color list.

JavaScript
const colors = ["red", "blue", "green", "yellow"];

const removed = colors.shift();

console.log(removed);
// "red"

console.log(colors);
// ["blue", "green", "yellow"]
Try It Yourself

How It Works

"blue" moves from index 1 to index 0. Length drops from 4 to 3.

Example 2 — Empty Array Returns undefined

Safe on empty arrays—no error, just no value.

JavaScript
const empty = [];

const result = empty.shift();

console.log(result);
// undefined

console.log(empty.length);
// 0
Try It Yourself

How It Works

Same as pop() on empty: returns undefined. Check length when empty state matters.

📈 Practical Patterns

Guards, queues, and start vs end removal.

Example 3 — Guard with length Before Shifting

Avoid processing when the queue is already empty.

JavaScript
const queue = ["task-a"];

let next;

if (queue.length > 0) {
  next = queue.shift();
  console.log("Processing:", next);
} else {
  console.log("Queue is empty.");
}
// Processing: task-a

console.log(queue);
// []
Try It Yourself

How It Works

Explicit length checks make queue-empty behavior clear in application code.

Example 4 — FIFO Queue with push() and shift()

Add at the back, remove from the front—first in, first out.

JavaScript
const queue = ["alice", "bob"];

queue.push("carol");

const served = queue.shift();

console.log(served);
// "alice"

console.log(queue);
// ["bob", "carol"]
Try It Yourself

How It Works

Alice joined first, so she is served first. Bob and Carol remain in order.

Example 5 — shift() vs pop() on the Same Array

See which end each method removes from.

JavaScript
const startQueue = ["a", "b", "c"];
const endStack = ["a", "b", "c"];

console.log("shift:", startQueue.shift());
// "a"

console.log("after shift:", startQueue);
// ["b", "c"]

console.log("pop:", endStack.pop());
// "c"

console.log("after pop:", endStack);
// ["a", "b"]
Try It Yourself

How It Works

shift + push = queue (FIFO). pop + push = stack (LIFO).

🚀 Common Use Cases

  • FIFO queues — process tasks in arrival order.
  • Breadth-first hints — dequeue next node from front of list.
  • Drain buffers — consume oldest items first.
  • Parser token streams — read next token from head.
  • Job schedulers — paired with push for simple queues.
  • Teaching opposites — contrast with pop and unshift.

🧠 How shift() Runs

1

Check length

Empty array returns undefined.

Guard
2

Capture index 0

Store first element as return value.

Read
3

Shift elements down

Move each item one index lower.

Re-index
4

Return removed value

Length decreased by one.

📝 Notes

  • Mutates the array—elements shift down, length - 1.
  • Empty array → undefined (no throw).
  • Slower than pop() on large arrays (O(n) re-indexing).
  • For immutable “drop first”, use arr.slice(1).
  • Pair with push for FIFO; with unshift for front inserts.
  • ES3—supported everywhere JavaScript runs.

Browser & Runtime Support

Array.prototype.shift() has been available since JavaScript 1.2 / ES3.

Baseline · ES3

Array.prototype.shift()

Supported in every browser ever shipped with JavaScript arrays, including Internet Explorer 3+, and all Node.js versions.

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.shift() Universal

Bottom line: Safe to use in any environment. No polyfill ever needed.

Conclusion

shift() removes the first element, re-indexes the rest, and returns what was removed. Use it with push() for simple FIFO queues, and prefer pop() when end removal is enough for performance.

Next, learn slice() to copy portions of an array without mutating.

💡 Best Practices

✅ Do

  • Use with push for FIFO queue behavior
  • Check length when empty queues matter
  • Store the return value when you need the removed item
  • Use slice(1) when immutability is required
  • Prefer pop if only end removal is needed

❌ Don’t

  • Shift huge arrays in tight loops without need
  • Assume shift throws on empty arrays
  • Forget mutation affects all references
  • Confuse shift (remove first) with unshift (add first)
  • Use shift when pop matches your logic

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.shift()

Remove the first element and shift the rest down.

5
Core concepts
📤 02

Returns

First item.

Output
03

Mutates

Re-indexes.

Side effect
🗃 04

FIFO

With push.

Pattern
05

Empty

undefined.

Edge case

❓ Frequently Asked Questions

shift() removes the first element from an array, shifts remaining elements down one index, shortens length by one, and returns the removed element.
Yes. shift() changes the array in place by removing index 0 and re-indexing the rest.
undefined. The array stays empty and length remains 0.
shift() removes from the start (first in, first out with push). pop() removes from the end. shift is slower on large arrays because every remaining element moves.
shift() removes the first element. unshift() adds one or more elements to the start. They are opposites at the front of the array.
shift() has been available since JavaScript 1.2 / ES3. It works in every browser including very old environments.
Did you know?

A JavaScript array used as a queue with push + shift can become slow when it grows very large, because every shift moves all remaining elements. For heavy queue traffic, dedicated queue structures or linked lists are common optimizations.

Continue to slice()

Learn how to extract a portion of an array without mutating the original.

slice() 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