You need to handover the array after shifting an item.

Array#shift returns/removes the item at index zero or undefined if the array is empty.

function del(arr) {
    if (arr.length === 0) return arr;
    arr.shift();
    return del(arr);
}

const arr = [1, 2, 3, 4];      // keep the variable/omit reassignment
                               // just to give proof

console.log(del(arr) === arr); // same object reference
console.log(arr);              // empty array

Answer from Nina Scholz on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › shift
Array.prototype.shift() - JavaScript | MDN
const array = [1, 2, 3]; const firstElement = array.shift(); console.log(array); // Expected output: Array [2, 3] console.log(firstElement); // Expected output: 1 ... The removed element from the array; undefined if the array is empty.
🌐
Mimo
mimo.org › glossary › javascript › array-shift
JavaScript Array shift: Syntax, Usage, and Examples
Mutates the Original Array: This ... First-Out (FIFO) data structure, perfect for managing queues. Safe on Empty Arrays: Calling shift() on an empty array will not cause an error....
🌐
W3Schools
w3schools.com › jsref › jsref_shift.asp
JavaScript Array shift() Method
The shift() method changes the original array. The shift() method returns the shifted element. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want ...
🌐
Lucee Documentation
docs.lucee.org › reference › functions › arrayshift()
array.shift() :: Lucee Documentation
pops the first element from an array and shift the rest of the values to the left. In case the array is empty an exception is thrown, unless the second argument "defaultValue" is provided, in that case that value is returned.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-shift-method
JavaScript Array shift() Method - GeeksforGeeks
September 18, 2024 - The shift() method in JavaScript is used to remove the first element of an array, reducing the array's length by one. This method is particularly useful for scenarios where elements need to be processed in the order they were added, such as ...
🌐
Medium
medium.com › an-idea › javascript-arrays-push-pop-shift-unshift-adc8fb815fc0
JavaScript Arrays: push(), pop(), shift() & unshift() | by Amanda M Johnson | An Idea (by Ingenious Piece) | Medium
October 10, 2021 - returns the removed element(s) and will return undefined if they array is empty · is also a destructive method and will mutate the original array · relies on the length property as well · Press enter or click to view image in full size · ...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › shift
JavaScript Array shift() - Remove First Element | Vultr Docs
November 25, 2024 - In this example, removing one element from the numbers array reduces its length from 5 to 4. Understand that applying shift() on an empty array returns undefined.
🌐
Sentry
sentry.io › sentry answers › javascript › how do i empty an array in javascript?
How do I Empty an Array in JavaScript? | Sentry
You can also loop through an array ... pop() or shift() methods, which remove the last element of an array and the first element of an array, respectively. ... This is the slowest of the methods described here. If you’re looking to get a deeper understanding of how JavaScript application ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript array shift method
JavaScript Array Shift Method
September 1, 2008 - The JavaScript Array.shift() method is used to remove the first element from an array and returns the removed element. This method reduces the length of the original array by one. When we call this method on the empty array, it returns "undefined".
🌐
Programiz
programiz.com › javascript › library › array › shift
JavaScript Array shift()
// removes the first element of ... method does not accept any arguments. Removes the first element from array and returns that value. Returns undefined if the array is empty....
Top answer
1 of 16
5711

Ways to clear an existing array A:

Method 1

(this was my original answer to the question)

A = [];

This code will set the variable A to a new empty array. This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array. You should be careful with this method because if you have referenced this array from another variable or property, the original array will remain unchanged. Only use this if you only reference the array by its original variable A.

This is also the fastest solution.

This code sample shows the issue you can encounter when using this method:

var arr1 = ['a','b','c','d','e','f'];
var arr2 = arr1;  // Reference arr1 by another variable 
arr1 = [];
console.log(arr2); // Output ['a','b','c','d','e','f']

Method 2 (as suggested by Matthew Crumley)

A.length = 0

This will clear the existing array by setting its length to 0. It also works when using "strict mode" in ECMAScript 5 because the length property of an array is a read/write property.

Method 3 (as suggested by Anthony)

A.splice(0,A.length)

Using .splice() will work perfectly, but since the .splice() function will return an array with all the removed items, it will actually return a copy of the original array. Benchmarks suggest that this has no effect on performance whatsoever.

Method 4 (as suggested by tanguy_k)

while(A.length > 0) {
    A.pop();
}

This solution is not very succinct, and it is also the slowest solution, contrary to earlier benchmarks referenced in the original answer.

Performance

Of all the methods of clearing an existing array, methods 2 and 3 are very similar in performance and are a lot faster than method 4. See this benchmark.

As pointed out by Diadistis in their answer below, the original benchmarks that were used to determine the performance of the four methods described above were flawed. The original benchmark reused the cleared array so the second iteration was clearing an array that was already empty.

The following benchmark fixes this flaw: http://jsben.ch/#/hyj65. It clearly shows that methods #2 (length property) and #3 (splice) are the fastest (not counting method #1 which doesn't change the original array).


This has been a hot topic and the cause of a lot of controversy. There are actually many correct answers and because this answer has been marked as the accepted answer for a very long time, I will include all of the methods here.

2 of 16
2755

If you need to keep the original array because you have other references to it that should be updated too, you can clear it without creating a new array by setting its length to zero:

A.length = 0;
🌐
Appdividend
appdividend.com › 2018 › 12 › 18 › javascript-array-shift-example-tutorial
JavaScript Array shift() Method
September 1, 2025 - const apps = ["Insta", "Fb", "Snap", ... value is found, it will remove each element of the array. If the array is empty before the call, it returns undefined....
🌐
Google Books
books.google.com › books
JavaScript: The Definitive Guide - David Flanagan - Google Books
JavaScript is a powerful, object-based scripting language; JavaScript programs can be embedded directly in HTML web pages. When combined with the Document Object Model (DOM) defined by a web browser, JavaScript allows you to create Dynamic HTML content and interactive client-side web applications.
🌐
You.com
codegrepper.com › code-examples › delphi › .shift+on+empty+array
You.com | AI Search Infrastructure for Enterprise Teams
Skip the groundwork with our AI-ready API platform and ultra-specific vertical indexes, delivering advanced search capabilities to power your next product.
🌐
Codegrepper
codegrepper.com › code-examples › c › shift+empty+array
shift empty array Code Example
shift array from index javascript · .shift() js · shift method javascript · es6 shift · javaascript shift · react input shift js · shift empty array · arr.shift() isift javascript · shift(); array. shift · nodejs shift array · shift in javascript ·