Array is most important keyword used in programming languages. Like other languages, Array is also used in javascript. Array in JavaScript is used to store multiple values in a single variable. Arrays in javascript is as special type of object. We can have variables of different types in the same Array. It may be object, function and array also.
Creating Array in javascript:
Syntax :
These are different types of syntax used to create array.
1 2 3 | [element0, element1, ..., elementN] new Array(element0, element1, ..., elementN) new Array(arrayLength) |
element0, element1, …, elementN :
A JavaScript array is initialized with the given elements.
arrayLength :
If the only argument passed to the Array constructor is an integer then it returns a new JavaScript array with length set to that number.
Note : Integer Number should be in range of between 0 and 2^32-1 (inclusive).
If the argument is any other number, a RangeError exception is thrown.
Example:
1 | var arr = ["apple", "banana", "mango"]; |
It creates arr name array object.
1 | var fruits = []; |
It creates fruits named array object without any element means empty array object
1 | var fruits = new Array( "apple", "banana", "mango" ); |
It creates fruits named array object with three elements.
We can use any of the above given method to create an array in javascript.
Accessing array elements
JavaScript arrays are zero-indexed: the first element of an array is at index 0, and the last element is at the index equal to the array’s length minus 1.
we use ordinal numbers to access an array as follows:
1 2 3 4 | var fruits = new Array( "apple", "banana", "mango" ); console.log(fruits[0]); // prints "apple" console.log(fruits[1]); // prints "banana" console.log(fruits[fruits.length - 1]); // prints "mango" // last element |
Setting array elements
we use ordinal numbers to set a value inside an array as follows:
1 2 3 | var fruits = new Array( "apple", "banana", "mango" ); fruits[3] = 'papaya'; // set 'papaya' as 4 elements in fruits object fruits[fruits.length] = 'orange' // set 'orange' as last element in fruits object |
Array.length property can be used for adding an element in last of an array object like above.
In above given example we set 4th element in fruits object but what will be happend if we add element at 6th element.
1 | fruits[5] = 'grapes'; // set 'grapes' as 6 elements in fruits object |
When setting a value on a JavaScript array when the value is a valid array index and that index is outside the current bounds of the array, the engine will update the array’s length property accordingly.
Array is object
Why Array is special type of object?
1. We can have variables of different types in the same Array. It may be object, function and array also.
1 2 3 | arr[0] = Date.now; arr[1] = FunctionName; arr[2] = vehicle; |
2. The typeof operator in JavaScript returns “object” for arrays but it’s elements can be accessed or assigned by using only numbers like:
1 2 3 4 5 6 | var fruits = new Array( "apple", "banana", "mango" ); fruits[0] or fruits["0"]; // returns apple fruits[3] = 'banana' ; fruits['liked'] = 'grapes' // using key |
3. But can not be accessed like objects
1 2 3 4 5 6 7 8 9 10 11 12 13 | var vehicle = {'wheels':2,'gear':4} var vehicle1 = []; vehicle1['wheels'] = 2; vehicle1['gear'] = 4; //vehicle's wheels can be accessed console.log(vehicle.wheels) //print 2 // but array object can not be accesses console.log(vehicle1.wheels) // gives error Instead we have to access it like console.log(vehicle1['wheels']) // prints 2 |
4. javascript array does not support Associated array (Named index array).
Associate Array : key – value pairs which has name as index.
1 | vehicle = ["wheels":2,"gears":4] // wrong statement. |
Even we can define it like
1 2 3 | var vehicle1 = []; vehicle1['wheels'] = 2; vehicle1['gear'] = 4; |
But can not be accessed using index number like
vehicle1[0]
will return undefined and vehicle1.length will return 0.
so if we want to define associated array in javascript then use object.