JavaScript Arrays
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
- Initialize it by using square bracket
- New Array keyword followed by parenthesis.
- Example for initializing array by using square bracket
var array= ['null',"array",`object`]
- Example for initializing array by using New Array keyword.
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