javascript empty array and push code example

Example 1: javascript append element to array

var colors= ["red","blue"];
	colors.push("yellow");

Example 2: js push array

var array = [];
var element = "anything you want in the array";
array.push(element); // array = [ "anything you want in the array" ]

Example 3: javascript array push

var fruits = [ "Orange", "Apple", "Mango"];

fruits.push("Banana");

Example 4: how to push an element into an array in javascript

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

// append new value to the array
arr.push("Hola");

console.log(arr);

Tags:

Ruby Example