Array.shift() Remove the first item

Last updated: 2026-09-09

Array.shift() Remove the first item

shift removes the first element and returns it; an empty array returns undefined. With unshift it forms a queue (FIFO). Note shift is O(n).

CategoryArrays
ES versionES1 (1997)

📝 Syntax

const removed = array.shift();

Returns:The removed first element, or undefined on an empty array.

Mutates the original:Yes (in place)

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

customer1
2
undefined
⚠️ Warning shift is O(n) — every remaining element shifts left. Prefer a deque for hot paths.
💡 Tip const [first, ...rest] = arr destructures the head without mutating.

🌐 Browser support

Browser ChromeChrome EdgeEdge FirefoxFirefox SafariSafari OperaOpera
Array.shift() v1 v12 v1.5 v3 v10.5
Supported by all modern browsers
More case studies coming soon — check back later.

📚 Related Tutorials

❓ FAQ

Qshift vs pop performance?
Apop is O(1); shift is O(n). Frequent shifts on big arrays become a bottleneck.
Qshift vs slice(1)?
Ashift mutates and returns the removed item; slice(1) returns a new array without mutating.
QHow to drain a queue safely?
Awhile (queue.length) { const item = queue.shift(); ... } — the loop ends naturally.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏