Skip to content

Latest commit

 

History

History

arrays

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

JavaScript - Arrays

Arrays in JavaScript will store heterogeneous values.

  • Create an array:

    const nameOfArray = [item1, item2, ...]; 
  • Accessing the elements:

    const avengers = ["Ironman", "Thor", "Thanos", 3000];
    let avenger = avengers[0]; 
  • Add element to the array:

    avengers.push("Captain"); //add at the end
    avengers[0] = "Captain"; //index based adding
  • Properties and Methods:

    avengers.length   // returns the number of avengers
    avengers.sort()   // sorts the avengers
    avengers.indexOf("ironman") // return index value of ironman
    avengers.pop() // delete last indexed value of avengers
  • NOTE: adding elements with high indexes can create undefined values in an array.