Languages
[Edit]
EN

JavaScript - add element to array

3 points
Created by:
Gaia-Kirby
630

In this article, we would like to show you how to add an element to an array in JavaScript.

1. Add element at the end of array

To add an element at the end of an array we use push() method.

Runnable example:

// ONLINE-RUNNER:browser;

var colors = ['Red', 'Yellow'];
colors.push('Green');

console.log(colors);

Output:

Red, Yellow, Green

2. Add element at the beginning of array

To add an element at the beginning of an array we use .unshift() method.

Runnable example:

// ONLINE-RUNNER:browser;

var colors = ['Red', 'Yellow'];
colors.unshift('Green');

console.log(colors);

Output:

Green,Red,Yellow

Alternative titles

  1. JavaScript - add value to existing array
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join