The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
Example
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
Output
[ 'C++', 'JavaScript', 'Python', 'Java', 'Lua' ] 5 [ 44, 10, 1.6, 12, 21, 35 ] 6
Recommended Readings: