Skip to content

JavaScript removes the first element from Array | Example code

  • by

Ues Array shift() method to remove the first item of an array in JavaScript. This method returns the removed element and changes the length of the array.

array.shift()

Remove the first item of the array in JavaScript

HTML example code. This should remove the first element, and then you can return the remaining:

If you have an array, the shift function shifts everything to the left.

<!DOCTYPE html>
<html>
<body>

  <script>
    var arr = [1, 2, 3, 4]; 
    var remEle = arr.shift(); // Removed Element == 1
    
    console.log(remEle)
    console.log(arr); 
  </script>

</body>
</html>

Output:

JavaScript removes the first element from Array

Another way, you could also use slice(1);

slice function doesn’t change the original Array.

var myarray = ["item 1", "item 2", "item 3", "item 4"];
  
alert(myarray.slice(1));

Do comment if you have any doubts or suggestions on this JS Array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *