array.unshift(item1, item2, ..., itemX)
The number of arguments to this function is the same as the number of elements to be added at the beginning of the array.
This function returns the new length of the array after inserting the arguments at the beginning of the array.
// app.js let apps = ["Instagram", "Facebook", "Messanger"]; apps.unshift("Oculus", "WhatsApp"); console.log(apps);
Run the file, and you can see that Oculus and WhatsApp are appended at the start of an array.
The following code will include almost all the scenarios of the unshift() method.
let arr = [1, 2]; arr.unshift(0); // result of call is 3, the new array length // arr is [0, 1, 2] arr.unshift(-2, -1); // = 5 // arr is [-2, -1, 0, 1, 2] arr.unshift([-3]); // arr is [[-3], -2, -1, 0, 1, 2]