Languages
[Edit]
EN

Javascript - add element to array

3 points
Created by:
Dirask Community
6900

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