Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

How to use array shift in JavaScript

Samia Ishaque

Overview

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()

Parameters

The shift function takes no mandatory inputs as parameters.

Return value

The shift function returns the element that has been removed from the array.

Example

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)

RELATED TAGS

javascript

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring