The shift
function in JavaScript removes the starting element of an array and returns it. The general syntax of the shift
function is as follows:
array.shift()
The shift
function takes no mandatory inputs as parameters.
The shift
function returns the element that has been removed from the array.
The example below will help you understand the shift
function better. First, we create the array and call the shift
function on it. We store the returned value in a variable and print to see that it is indeed the first element of the array.
Moreover, printing the array shows that the first element has been removed.
const arr1 = [1,2,3]const first = arr1.shift()console.log(first)console.log(arr1)
Free Resources