The shift()
method removes the first item of an array.
Note: This method changes the length of the array and The return value of the shift method is the removed item.
Note: this method will change the original array.
To remove the last item of an array, use the pop() method.
Let's Start
Example(1)
1.Create array.
var languages = ["PHP", "JAVASCRIPT", "PYTHON", "C++"];
document.getElementById("demo").innerHTML = languages;
function languages_function() {
languages.shift();
document.getElementById("demo").innerHTML = languages;
}
<!DOCTYPE html>
<html>
<head>
<title>How To Remove First Array Element In JavaScript With Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<br>
<div class="text-center">
<h1>Removing First Array Element In JavaScript</h1>
</div>
<br>
<div class="well">
<button class="btn btn-primary" onclick="languages_function()">Click it</button>
<h1 id="demo"></h1>
</div>
<br>
</div>
</body>
</html>
<script>
var languages = ["PHP", "JAVASCRIPT", "PYTHON", "C++"];
document.getElementById("demo").innerHTML = languages;
function languages_function() {
languages.shift();
document.getElementById("demo").innerHTML = languages;
}
</script>