Home >>JavaScript Array Reference >JavaScript Array shift() Method

JavaScript Array shift() Method

JavaScript Array shift() Method

JavaScript shift() method is used to remove the first item of the given input array. It changes the length of the given array. It changes the original given array. It returns the value of the removed item.

Syntax:
array.shift()

Browser Support

Method Chrome Edge Firefox Safari Opera
shift() Yes Yes Yes Yes Yes
Here is an example of JavaScript shift() method:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var x = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
document.getElementById("demo").innerHTML = x;
function myFunction() 
{
 x.shift();
 document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

Example 2:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
var y = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
document.getElementById("demo1").innerHTML = y;
function myFunction1() 
{
  y.shift();
  document.getElementById("demo1").innerHTML = y;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.


No Sidebar ads