Skip to content

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 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: