Array
Array
Overview
The array is an ordered collection of data(can be primitive or non-primitive) used to
store multiple values. This helps in storing an indefinite number of values.
Each item/value has an index attached to it, using which we can access the values. In
JavaScript index starts at 0.
The array also contains a property length that stores the number of elements present
inside the array. It changes its value dynamically as the size of the array changes.
Creating an Array
There are two ways to create an array -
var arrayName = [ ] ;
1
3. Providing the values
● Using Array indexes - Create an empty array, and then provide the elements.
When a negative value is used, the array stores the element as a key-value pair, where
the negative index is the key and the element to be inserted is the value.
❖ Initially, the array has a size of 3; now, the 10 is pushed at the 5th index,
size becomes 6, and the remaining index is filled with undefined values
2
Accessing Element in Array
You can access the individual elements of the array using the square bracket notation.
NOTE : When using an array inside an array, you can access the value of the inner array
directly
arr[2][1] ; // return 3
arr[5] ; // return undefined
arr[-1] ; //return undefined
If you access the array outside its range, i.e., pass an invalid index(whether negative or
greater than the array’s length), then undefined is returned.
Heterogeneous in Nature
It can contain different types of values at the same time. Also, the array can store
primitive and non-primitive values.
3
Functions on Arrays
1. push Method :
The push( ) method adds one or more elements to the end of the array and returns
the new length of the array. It uses the length property to add elements.
If the array is empty, the push( ) method will create the length property and add an
element.
In case you are adding multiple elements, separate them using a comma(, ).
arr.push(5,6) ;
❖ arr becomes [1 , 2 , 3 , 4 , 5 , 6]
2. pop Method :
The pop() method is used to remove the last element from the array and return
that element. It also decreases the length of the array by 1.
Using pop on an empty array returns 'undefined'.
3. shift Method :
The shift( ) method is used to remove the first element from an array and return
that element.
If the length property is 0, i.e. empty array, then undefined is returned.
var arr = [ ] ;
arr.shift( ) ; // returns undefined
4
4. unshift Method :
The unshift( ) method is used to add one or more elements to the beginning of the
array and returns the new length of array.
In case, you are adding multiple elements, separate them using a comma( , ).
arr.unshift( 10 ) ;
❖ arr becomes [10 , 0 , 1 , 2 , 3 ]
5. indexOf Method :
The indexOf( ) method is used to return the first index at which the given element is
found in the array. If the element is not found, then -1 is returned.
By default, the whole array is searched, but you can provide the start index from which
the search should begin. It is optional. If the index provided is negative, the offset is set
from the end of the array, and a search in the opposite direction is done.
6. splice Method :
5
7. reverse Method :
The reverse method is used to reverse the content of the array and return the new
reversed array. This means that the first element becomes last and vice-versa.
8. sort Method :
The sort( ) method is used to sort the elements of an array and return the sorted
array. The sort is done by converting the elements to string and then comparing them.
9. join Method
The join( ) method is used to concatenate all the elements in an array and return a
new string.
They are separated by comma(, ) by default, but you can also provide your separator as
a string. It is optional to provide a separator.
6
10.toString Method
The toString( ) method is used to return the array in the form of a string. The string
contains all the elements separated by a comma.
We do not need to provide the separator as a parameter as we did in the join method.
1. for loop: It is used commonly to iterate over all the values of the array.
2. forEach Method : The forEach( ) method calls a function once for each array
element.
You can either provide a function definition as shown in the syntax above. Or
you can pass the function name to it.
7
Example : var items = [ 1 , 2 , 3 ] ;
items.forEach( function(item) {
console.log ( item * 10 );
}) ;
Output : 10
20
30