JavaScript Arrays

An array is a linear data structure and collection of items stored at a contiguous memory
location. Pairs of square brackets [ ] represent an array and all elements are comma (,)
separated.

An Array can hold multiple values(elements) of any type i.e (String, Number, Boolean, objects,
and even other Arrays ) under a single name.
Arrays are zero-based indexing. The position of the element is called the index.

In below example array contains four elements of type String, Number, Boolean, and Object
eg. const array = [ “abc” , 123, true, {} ]; // this is how we can create array using an
array literal
-> Another way to create an array is using Array Constructor.
eg. const array = new Array(“abc” , 123, true, {} );
console.log( array[0] ); // output will be `abc` or we can say the first element at 0th
the index will be printed.

2. Arrays in javaScript are mutable and can be changed freely

eg. const outArray = [50, 40, 60 ] ;
outArray [0] = 15 ; // changing first element to ' 15 '
now outArray value is [ 15, 40, 60]

3. Nested Array (Multi-dimensional array)
Array or arrays inside Array. Or we can say that A multi-dimensional Array is an array
containing one or more arrays.
eg. const teams = [ [ “Delhi”,225] ,[“Punjab”,350] ];
In the above example, we have one array “teams” and inside the “teams ” array we have two
more arrays

1

-> at 0th index i.e [ "Delhi",225]
-> at 1st index i.e ["Punjab",350]

Access multi-dimensional array:-
const m_Array = [

[ 1, 2, 3], // ( 0th index of m_Array)
[ 4, 5, 6], // ( 1st index of m_Array )
[7, 8,9], // ( 2nd index of m_Array)
[ [ 10, 11, 12], 13, 14] // ( 3rd index of m_Array)

];
OUTPUTS:- let's iterate m_Array
console.log( m_Array [3] ); // output will be [ [10, 11, 12], 13, 14 ]
console.log( m_Array[3][0] ); // output will be [10, 11, 12 ]
console.log(m_Array[3][0][1] ) ; // output will be 11

When we use [ ] to access an array, the first set of brackets refers to the parent array (the first level
or in the outermost array ) and each additional bracket refers to the next level of entries inside.

Leave a Reply