Site icon i2tutorials

JavaScript Arrays 

js2

 

What is an array?

An array is a collection of similar type of data and is used to store different elements in a single variable.

For example, if we want to store the names of 45 people then we can create an array to store 45 names.

How to declare array?

There are 2 ways to declare the array

  1. Initialize it by using square bracket
  2. New Array keyword followed by parenthesis.
var array= ['null',"array",`object`]
var array= new Array ('null',"array",`object`)
console.log(typeof(array)); // object

How to access individual elements in an Array?

Array can be accessed by using index value.

var array= ['null',"array",`object`]

array[0] =null           First Element

array[1] = array       Second Element

array[2] = object      Third Element

 

Index value starts from 0

Index value = (no of elements in a array) - 1

 

Exit mobile version