How to append something to an array?

Created by jdecuyperLinked to 5.2m issues across 294 teams


Here's how to append values to the end of an array in JavaScript:

First, let's initialize an array with some values:

// initialize array var arr = ["Hi", "Hello", "Bonjour"];

Now, we can use the Array.prototype.push method to append values to the end of the array:

// append new value to the array arr.push("Hola"); console.log(arr); // ["Hi", "Hello", "Bonjour", "Hola"]

We can also use the push() function to append more than one value to an array in a single call:

// initialize array var arr = ["Hi", "Hello", "Bonjour", "Hola"]; // append multiple values to the array arr.push("Salut", "Hey"); // display all values for (var i = 0; i < arr.length; i++) { console.log(arr[i]); }
We use cookies to personalize content
run ads, and analyze traffic.