Javascript Array push()

Introduction

Javascript array push() method adds one or more elements to the end of an array.

It returns the new length of the array.

arr.push(element1[, ...[, elementN]])
  • elementN - the element(s) to add to the end of the array.
let langs = ['CSS', 'Java']
let total = langs.push('HTML', 'Javascript')

console.log(langs);  /*  w w w  .  j  a v  a 2  s  . c o  m*/
console.log(total);

More examples

let colors = ["red", "blue"];
colors.push("brown"); // add another item 
colors[3] = "black"; // add an item 
console.log(colors.length); // 4 

let item = colors.pop(); // get the last item 
console.log(item); // "black" 



PreviousNext

Related