Home >>Javascript Tutorial >Javascript Array

Javascript Array

Javascript Array

An object that represents a collection of similar type of elements is known as JavaScript Array.
Here are the three ways by which arrays can be created in JavaScript.

  • Array literal
  • Array directly (using new keyword)
  • Array constructor (using new keyword)

Array Literal in JavaScript

The values are placed in [] and are separated by , (comma) in the syntax for creating array using array literal in JavaScript.
Here is the syntax for the array creation using array literal in JavaScript:

var arrayname=[value1,value2.....valueN];  

Please note that the .length property returns the length of an array.

Here is an example depicting the creation of arrays using array literal in JavaScript:

<script>  
var emp=["PHP","Java","Python"];  
for (i=0;i<emp.length;i++)
{  
document.write(emp[i] + "<br/>");  
}  
</script>  
Output :
PHP
Java
Python

Here is another example to sum of array's element:

<script>  
var arr=[10,11,12,13,14,15];
var sum=0;  
for (i=0;i<arr.length;i++)
{  
sum=sum+arr[i];
}  
document.write("Sum of array="+sum);  
</script>  
Output :Sum of array=75

Array Directly in JavaScript

In Array Directly, a new keyword is used to create instance of array.
Here is the syntax of array directly

var arrayname=new Array();  

Here is an example for array directly:

<script>  
var i;  
var emp = new Array();  
emp[0] = "Tokyo";  
emp[1] = "Japan";  
emp[2] = "Heisenberg";  
  
for (i=0;i<emp.length;i++){  
document.write(emp[i] + "<br>");  
}  
</script>
Output :
Tokyo
Japan
Heisenberg

Array constructor (new keyword) in JavaScript

Instance of arrays are created by passing arguments in constructor to avoid the need of providing value explicitly.

Here is an example to understand it better:

<script>
var emp=new Array("PHP","Python","Java");  
for (i=0;i<emp.length;i++){  
document.write(emp[i] + "<br>");  
}  
</script>
Output :
PHP
Python
Java

Associative Array in JavaScript

In JavaScript, arrays with named indexes are not supported. Arrays are known to use numbered indexes however, objects in JavaScript use named indexes.
Let's understand it in simple words, when values are assigned to the keys in a variable of array, it transforms the array in to an object and loses the attributes and methods of an array.
These types of arrays are known as Associative Arrays in JavaScript.

Here is an Example to understand the Associative array:

<script>
var arr = new Object();
arr["one"] = 1;
arr["two"] = 2;
arr["three"] = 3;
for(var i in arr)
{
     document.write(i + "=" + arr[i] + '
');
}
</script>
Output :
one=1
two=2
three=3

Multi-Dimensional Array

In multi-Dimensional arrays, an array is used inside of an array.
Multidimensional arrays are known to have more than 2 dimensions.
In most cases, 2 dimensions are enough though, there are some places where 3 dimensions can be used like during 3D operations, physics calculations, etc.

Let's understand the multi-dimensional arrays with an example:

<script>
var array=Array(Array(10,11),Array(12,13))
for (var i = 0; i < array.length; i++) 
{ 
	for (var j = 0; j < array[i].length; j++) 
	{ 
	document.write(array[i][j]+" ");	
	}   
	document.write("<br>");
} 
</script> 
 
Ooutput:
10 11
12 13

Let's understand through 2nd example

 <script>
 // Create one dimensional array 
  var arr = new Array(2); 
document.write("Creating 2D array <br>"); 
// Loop to create 2D array using 1D array 
for (var i = 0; i < arr.length; i++) 
{ 
    arr[i] = new Array(2); 
} 
  
var h = 0; 
  
// Loop to initilize 2D array elements. 
for (var i = 0; i < 2; i++) 
{ 
    for (var j = 0; j < 2; j++) 
	{ 
        arr[i][j] = h++; 
    } 
} 
  
// Loop to display the elements of 2D array.  
for (var i = 0; i < 2; i++) 
{ 
    for (var j = 0; j < 2; j++)   
	{ 
        document.write(arr[i][j] + " "); 
    } 
    document.write("<br>"); 
}
</script> 
 
Ooutput:
Creating 2D array
0 1
2 3