Array unshift() Method
The Array unshift()
method adds one or more elements to the beginning of an array and returns the length of the new array. The syntax for this method is
array.unshift(element1, element2, ..., elementN);
We consider an array named os
consisting of four elements
var os = ["Debian", "Fedora", "openSUSE", "Ubuntu"];
Using the unshift()
method, we will add the element CentOS
to the beginning of the array and assign the returned length of the new array to the variable length
var length = os.unshift("CentOS");
The new modified array with one new element at its beginning becomes
["CentOS", "Debian", "Fedora", "openSUSE", "Ubuntu"];
and the variable length
holds the length of the new array, which is 5.