The unshift()
method adds one or more elements to the beginning of an array and returns the new length of the array.
Example
let languages = ["Java", "Python", "C"];
// add "JavaScript" at the beginning of the array
languages.unshift("JavaScript");
console.log(languages);
// Output: [ 'JavaScript', 'Java', 'Python', 'C' ]
unshift() Syntax
The syntax of the unshift()
method is:
arr.unshift(element1, element2, ..., elementN)
Here, arr is an array.
unshift() Parameters
The unshift()
method takes in an arbitrary number of elements to add to the array.
unshift() Return Value
- Returns the new (after adding arguments to the beginning of array) length of the array upon which the method was called.
Notes:
- This method changes the original array and its length.
- To add elements to the end of an array, use the JavaScript Array push() method.
Example: Using unshift() method
var languages = ["JavaScript", "Python", "Java", "Lua"];
var count = languages.unshift("C++");
console.log(languages); // [ 'C++', 'JavaScript', 'Python', 'Java', 'Lua' ]
console.log(count); // 5
var priceList = [12, 21, 35];
var count1 = priceList.unshift(44, 10, 1.6);
console.log(priceList); // [ 44, 10, 1.6, 12, 21, 35 ]
console.log(count1); // 6
Output
[ 'C++', 'JavaScript', 'Python', 'Java', 'Lua' ] 5 [ 44, 10, 1.6, 12, 21, 35 ] 6
Also Read: