In this example, JavaScript unShift will add -1 and -2 in the beginning of array arr. So the final array will become like [-1,-2,1,2]. See the code snippet:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="addElement()">Add element</button>
<p id="sample"></p>
<script>
var arr = [1, 2];
document.getElementById("sample").innerHTML = arr;
function addElement() {
arr.unshift(-1,-2);
document.getElementById("sample").innerHTML = arr;
}
</script>
</body>
</html>