shift
And unshift
Examples¶shift
¶The general syntax for this method is:
arrayName.shift()
This method removes and returns the FIRST element in an array.
No arguments are placed inside the parentheses ()
.
Example
1let arr = ['a', 'b', 'c', 'd', 'e'];
2
3arr.shift();
4
5console.log(arr);
Output
'a'
['b', 'c', 'd', 'e']
unshift
¶The general syntax for this method is:
arrayName.unshift(item1, item2, ...)
This method adds one or more items to the START of an array and returns the new length.
The new items may be of any data type.
Example
1let arr = ['a', 'b', 'c'];
2
3arr.unshift('hello', 121);
4
5console.log(arr);
Output
5
['hello', 121, 'a', 'b', 'c']