Skip to content

Array push()

JavaScript Array push() Definition

The push () method adds one or more elements to the end of an array. And it returns its new length. This changes the number and length of elements of the array.
Although strings are natural, the strings are not invariant because they are array-like objects, but are not suitable for applications in this method. Similarly, the Array-like object also applies to arguments.

Syntax:

array.push(item1, item2, …, itemX)

item1, item2, …, itemX( required ): The items to add to the array


JavaScript Array push() Examples

Example 1

        let names = ["john", "alex"];

        let arr1 = names.push("oliver");
        console.log(arr1);

        let arr2 = names.push("bob");
        console.log(arr2);

output:

3
4


Example 2

        let numbers = [1,2,3];

        console.log(numbers.push(4,5))
        console.log(numbers);

output:

5
[1, 2, 3, 4, 5]


Example 3

        let animal = ["cat", "horse"];
        let newAnimals = ["dog","donkey"]
        animal.push(newAnimals)

        console.log(animal)

output:

0: “cat”
1: “horse”
2: (2) [“dog”, “donkey”]


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: