add new element in javascript array code example

Example 1: javascript append to array

var colors=["red","white"];
colors.push("blue");//append 'blue' to colors

Example 2: js push array

array.push(element_to_push);

Example 3: js push array

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

Example 4: how to make and add to an array in javascript

var arrayExample = [53,'Hello World!'];
console.log(arrayExample) //Output =>
[53,'Hello World!']

//You can also do this
arrayExample.push(true);

console.log(arrayExample); //Output =>
[53,'Hello World!',true];

Tags:

Java Example