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
Fundamentals
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.
Concept
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.
Foundation
📝 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.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Remove first element
arr.shift()
Return value
Removed element or undefined
Remove last element
arr.pop()
Add to front
arr.unshift(x)
Mutates array?
Yes
Compare
📋 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
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.
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.
Important
📝 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.
Compatibility
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 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.shift()Universal
Bottom line: Safe to use in any environment. No polyfill ever needed.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.shift()
Remove the first element and shift the rest down.
5
Core concepts
📝01
Syntax
arr.shift()
API
📤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.