Array shift()

JavaScript Array shift() Definition

The shift () method removes the first element of the array. Returns the removed item. This method changes the length of the array. If the length of the array is zero, it returns the value of undefined.

Syntax:

array.shift()

Let’s understand the logic with a small example:

    let names = ["john", "rick", "bob"];
    console.log(names);

    let remove = names.shift();

    console.log("remove: " + remove)

    console.log("new array: "+names)

[“john”, “rick”, “bob”]
remove: john
[“rick”, “bob”]


JavaScript Array shift() Example

        let list = [];
        let newList = list.shift();
        console.log(newList);
        console.log(list);

        let pushList = list.push("a", "b", "c");
        console.log("number of elements: " + pushList)
        console.log("new array: " + list)

output:

undefined
[]
number of elements: 3
new array: a,b,c

Depending on this example I am writing this:

        let arr = list.shift();
        console.log(list)

output:

[“b”, “c”]


Browser Support

Chrome1
Edgeyes
Firefox1
Internet Explorer5.5
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

2 thoughts on “Array shift()

Add yours

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started