The easiest way is using shift(). If you have an array, the shift function shifts everything to the left.
var arr = [1, 2, 3, 4];
var theRemovedElement = arr.shift(); // theRemovedElement == 1
console.log(arr); // [2, 3, 4]
Answer from Thalsan on Stack OverflowW3Schools
w3schools.com › jsref › jsref_shift.asp
JavaScript Array shift() Method
❮ Previous JavaScript Array Reference Next ❯ · Shift (remove) the first element of the array: const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift(); Try it Yourself » · The shift() method returns the shifted element: const ...
Top answer 1 of 5
549
The easiest way is using shift(). If you have an array, the shift function shifts everything to the left.
var arr = [1, 2, 3, 4];
var theRemovedElement = arr.shift(); // theRemovedElement == 1
console.log(arr); // [2, 3, 4]
2 of 5
150
Just use arr.slice(startingIndex, endingIndex).
If you do not specify the endingIndex, it returns all the items starting from the index provided.
In your case arr=arr.slice(1).
Videos
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › shift
Array.prototype.shift() - JavaScript | MDN
The shift() method is a mutating method. It changes the length and the content of this. In case you want the value of this to be the same, but return a new array with the first element removed, you can use arr.slice(1) instead.
Top answer 1 of 10
295
The .shift() method removes the first element of an array. Then you can return the remaining:
const array = ["item 1", "item 2", "item 3", "item 4"];
array.shift();
console.log(array);
As others have suggested, you could also use slice(1):
const array = ["item 1", "item 2", "item 3", "item 4"];
console.log(array.slice(1));
2 of 10
100
Why not use ES6?
const array = ["item 1", "item 2", "item 3", "item 4"];
const [, ...rest] = array;
console.log(rest)
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › shift
JavaScript Array shift() - Remove First Element | Vultr Docs
November 25, 2024 - Validate the changes to the array. ... let fruits = ['apple', 'banana', 'cherry']; let removedElement = fruits.shift(); console.log(removedElement); // Outputs: apple console.log(fruits); // Outputs: ['banana', 'cherry'] Explain Code · This code demonstrates removing the first element from the fruits array.
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to remove the first element from an array in javascript
How to Remove the First Element from an Array in JavaScript
January 10, 2024 - Explore efficient methods to remove the first element from an array in JavaScript with our comprehensive guide. Learn through clear examples and in-depth explanations to master the art of array manipulation. Discover the power of Array.shift(), Array.slice(), and Array.splice(). How to Remove the First Element from an Array in JavaScript - your key to JavaScript array mastery!
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › splice
Array.prototype.splice() - JavaScript | MDN
If deleteCount is omitted, or if its value is greater than or equal to the number of elements after the position specified by start, then all the elements from start to the end of the array will be deleted. However, if you wish to pass any itemN parameter, you should pass Infinity as deleteCount to delete all elements after start, because an explicit undefined gets converted to 0. If deleteCount is 0 or negative, no elements are removed.
Sololearn
sololearn.com › en › Discuss › 496498 › remove-the-first-element-of-an-array
Remove the first element of an array | Sololearn: Learn to code for FREE!
Quick view: var empArr=[]; var maxArr = 50; var s; for(var i=0; i<maxArr; i++) { empArr.push(Math.round(Math.random() * 50)); document.write("Element " + i + ": " + empArr[i] + "<br>"); } document.write("<br>"); arr(); function arr(){ if(empArr.length>=50){ empArr.shift(); } for(var i=0; i<empArr.length; i++) { document.write("NEW Element " + i + ": " + empArr[i] + "<br>"); } }
Top answer 1 of 16
161
Copyfruits.shift(); // Removes the first element from an array and returns only that element.
fruits.pop(); // Removes the last element from an array and returns only that element.
See all methods for an Array.
2 of 16
116
Creates a 1 level deep copy.
Copyfruits.slice(1, -1)
Let go of the original array.
Thanks to @Tim for pointing out the spelling errata.
JSchallenger
jschallenger.com › javascript-practice › javascript-arrays › remove-first-elements-array-javascript
Remove first n elements of an array | JSchallenger
JSchallenger. Write a function that takes an array (a) as argument. Remove the first 3 elements of 'a'. Return the result