Array.prototype.shift()

shift()
Returns: any · Updated March 14, 2026 · Array Methods
array shift mutator

shift() removes and returns the first element of an array. It modifies the array in place, reducing its length by 1 and shifting all other elements to a lower index.

Syntax

arr.shift()

Takes no parameters. Returns the removed element, or undefined if the array is empty.

Examples

Basic usage

const fruits = ["apple", "banana", "cherry"];
const first = fruits.shift();

console.log(first);
// "apple"

console.log(fruits);
// ["banana", "cherry"]

Working with empty arrays

const empty = [Global_Objects::eval];
const result = empty.shift();

console.log(result);   // undefined
console.log(empty);    // [Global_Objects::eval]

Common Patterns

Using shift() in a loop

Process elements from the beginning:

function processQueue(queue) {
  while (queue.length > 0) {
    const item = queue.shift();
    console.log("Processing:", item);
  }
}

const tasks = ["task1", "task2", "task3"];
processQueue(tasks);
// Processing: task1
// Processing: task2
// Processing: task3

Implementing a queue

shift() is commonly used to implement a queue data structure (FIFO - first in, first out):

class Queue {
  constructor() {
    this.items = [Global_Objects::eval];
  }
  
  enqueue(item) {
    this.items.push(item);
  }
  
  dequeue() {
    return this.items.shift();
  }
  
  peek() {
    return this.items[0];
  }
}

const queue = new Queue();
queue.enqueue("first");
queue.enqueue("second");
console.log(queue.dequeue()); // "first"
console.log(queue.dequeue()); // "second"

Converting arguments to array

Before rest parameters existed, arguments were converted to array using Arrayslice.call() or shift():

function example() {
  const args = Arrayslice.call(arguments);
  console.log(args);
}

example(1, 2, 3);
// [1, 2, 3]

Behavior Notes

  • shift() returns undefined on empty arrays
  • This method modifies the original array (mutating method)
  • All remaining elements shift down by one index

See Also