JavaScript Array shift() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.shift() removes the first element, re-indexes the rest, shortens length by one, and returns the removed value. It mutates the array. Pair with push() for a simple FIFO queue.

Remember
array.shift()

["a", "b", "c"].shift()  →  "a"
array becomes            →  ["b", "c"]

empty array → undefined (no throw)
mutates the array ✓

shift = remove front
pop   = remove end
unshift = add front

Syntax

Call array.shift. Start with this line.

JavaScript
array.shift()
CallWhat it does
array.shift()Primary signature.
array.shift(...)Call with the arguments your case needs.

Built into JavaScript. Call shift(...) on an array instance — no import required.

Library declarations
JavaScript
array.shift()

Parameters

ParameterDescription
ArrayRequired. Source that will lose its first element

Return Value

Return ValueDescription
*Removed element, or undefined if empty

JavaScript Examples

Example 1 — Remove the First Element

Shift "red" off a color list and inspect what remains.

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

  1. Start with four colors. "red" sits at index 0.
  2. Call shift. It removes index 0 and returns "red".
  3. Everything slides left. "blue" becomes the new first item.
  4. Length drops. The array is now length 3.

Example 2 — Empty Array Returns undefined

shift() is 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

  1. Nothing to remove. Length is already 0.
  2. Return value. shift() yields undefined — same idea as pop() on empty.
  3. Array unchanged. It stays []; length remains 0.
  4. Guard tip. Check arr.length > 0 when empty means “do nothing.”

Example 3 — 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

  1. Start the line. Alice and Bob are already waiting.
  2. Enqueue. push("carol") adds Carol at the end.
  3. Dequeue. shift() serves Alice — she joined first.
  4. Order preserved. Bob and Carol remain in arrival order.

Browser & Runtime Support

Array.prototype.shift() has been part of JavaScript since ES3 / JavaScript 1.2, alongside push, pop, and unshift.

✓ Baseline · ES3

Array.prototype.shift()

Supported in every major browser (including Internet Explorer) and all Node.js versions. No polyfill required.

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

Bottom line: Safe everywhere. Use it freely for queues and front removal; for hot paths on huge arrays, consider designing around pop() instead.

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. It does not throw.
shift() removes from the start. pop() removes from the end. With push, shift makes a FIFO queue; with push, pop makes a stack (LIFO).
shift() removes the first element. unshift() adds one or more elements at 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.

Next: slice()

Learn how to copy a portion of an array without changing 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.

11 people found this page helpful