OneBite.Dev - Coding blog in a bite size

append item in array in Javascript

Code snippet on how to append item in array in Javascript

  var colors = ["red", "yellow", "green"]; 
  colors.push("blue");  // append "blue" at the end of colors

This code uses the push method to append an item (“blue”) to the end of an array (colors). First, the colors array is declared with three elements as strings: “red”, “yellow” and “green”. Then the push method is used to add a new element, “blue”, to the colors array. This adds “blue” to the end of the array, expanding it to four elements. The array now reads “red”, “yellow”, “green”, “blue”.

javascript