The unshift() method adds new items to the beginning of an array, and returns the new length.
Note: This method changes the length of an array.
To add new items at the end of an array, use the push() method.
Syntax and Usage
array.unshift(item1, item2, ..., itemX)
Parameter Values
item1, item2, ..., itemX -> Required. The item(s) to add to the beginning of the array
var languages = ["PHP", "JAVASCRIPT", "PYTHON", "C++"];
document.getElementById("demo").innerHTML = languages;
function languages_function() {
languages.unshift("HTML","CSS");
document.getElementById("demo").innerHTML = languages;
}
<!DOCTYPE html>
<html>
<head>
<title>adds new items to the beginning of an array, and returns the new length.</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>adds new items to the beginning of an array, and returns the new length.</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.unshift("HTML","CSS");
document.getElementById("demo").innerHTML = languages;
}
</script>