The shift() method removes the first element from an array and returns that element.
Example
shift() Syntax
The syntax of the shift() method is:
arr.shift()
Here, arr is an array.
shift() Parameters
The shift() method does not accept any arguments.
shift() Return Value
- Removes the first element from
arrayand returns that value. - Returns
undefinedif the array is empty.
After removing the element at the 0th index, it shifts other values to consecutive indexes down.
Notes:
- This method changes the original array and its length.
- To remove the last element of an array, use the JavaScript Array pop() method.
Example: Using shift() method
Output
[ 'Python', 'Java', 'C++', 'Lua' ] JavaScript [ 1, 2, 3 ] [ [ 4, 5, 6 ], [ -5, -4, -3 ] ]
Recommended Readings: