Mar 1, 2022
The shift()
function removes the first element from an array and returns the removed element.
As a result, the length of the array changes as well.
const array = [1, 2, 3, 4, 5];
array.shift();
array;
If the array is empty, shift()
will return undefined
and not modify the array.
const array = [];
array.shift();
shift()
, in combination with push()
, can be used to make an array act like a queue as shown below.
const array = [];
function next(array) {
return array.shift();
}
function enqueue(array, val) {
array.push(val);
}
enqueue('First');
enqueue('Second');
next();
array;
next();