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
1 2 3 4 5 | let arr = ['a', 'b', 'c', 'd', 'e'];
arr.shift();
console.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
1 2 3 4 5 | let arr = ['a', 'b', 'c'];
arr.unshift('hello', 121);
console.log(arr);
|
Output
5
['hello', 121, 'a', 'b', 'c']