push array mdn code example

Example 1: js push array

array.push(element_to_push);

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: js array push

const arr = ["foo", "bar"];
arr.push('baz'); // 3
arr; // ["foo", "bar", "baz"]

Example 4: js push

let arr = [9, 4, 3, 18]
arr.push(30)
console.log(arr) // [9, 4, 3, 18, 30]

Example 5: push javascript

/*The push() method adds elements to the end of an array, and unshift() adds
elements to the beginning.*/
let twentyThree = 'XXIII';
let romanNumerals = ['XXI', 'XXII'];

romanNumerals.push(twentyThree);
// now equals ['XIX', 'XX', 'XXI', 'XXII', 'XXIII']

romanNumerals.unshift('XIX', 'XX');
// now equals ['XIX', 'XX', 'XXI', 'XXII']

Tags:

Misc Example