Java Script


JavaScript Tutorial


Admission Enquiry Form


What is an Array in JavaScript ?

An array is a technique, which is used to store more than one value at a time.


Syntax of creating an Array:

var ArrayName=[item1,item2,item3,.....];

The ways to create an array is:

1. Using an array literal

The first way to create an array is by using an array literal []. For example,

var array1 = ["HTML","CSS","JavaScript"];

2. Using the new keyword

The second way to create an array is by using 'new' keyword. For example,

var array2 = new Array("Java","PHP","Python");

In the above both examples, we have created an array having three elements.Array literals is the traditional method to create an array.


Some more examples to create an array are as follows:

//empty array
var arr = [];

//numeric array
var marks= [10,20,30,40,50];

//string array
var str = ["first","second","third"];

//boolean array
var boolArray = [true,false];

//Array with decimal numbers
var value = [12.34,44.54,34.56];

//an array with mixed data types
var info= ["compuhelp",1999,1,true,77.98];

Example1 of an Array:

<html>
<head>
<title>Array</title>
</head>
<body>
<script>
var marks=[10,20,30,40,50];
document.write(marks[0]+"<br>");
document.write(marks[1]+"<br>");
document.write(marks[2]+"<br>");
document.write(marks[3]+"<br>");
document.write(marks[4]);
</script>
</body>
</html>

Output:

10
20
30
40
50


Example2 of an Array using loop:

<html>
<head>
<title>Array</title>
</head>
<body>
<script>
var lang=["C","C++","HTML","CSS","JavaScript"];
var i,len;
len=lang.length;
for(i=0;i<len;i++)
{
document.write(lang[i]+"<br>");
}
</script>
</body>
</html>

Output:

C
C++
HTML
CSS
JavaScript


Example of an Array using new keyword:

<html>
<head>
<title>Array</title>
</head>
<body>
<script>
var string_arr = new Array();
var i,len;
string_arr[0] = "html";
string_arr[1] = "css";
string_arr[2] = "javascript";
string_arr[3] = "php";
len=string_arr.length;
for(i=0;i<len;i++)
{
document.write(string_arr[i]+" ");
}
</script>
</body>
</html>

Output:

html css javascript php


Example of an Array using prompt() function:

<html>
<head>
<title>Array with user input</title>
</head>
<body>
<script>
var marks=new Array(5);
var i,len;
len=marks.length;
for(i=0;i<len;i++)
{
marks[i]=prompt("Enter marks :");
}
for(i=0;i<len;i++)
{
document.write(marks[i]+"<br>");
}
</script>
</body>
</html>

Output:

10
20
30
40
50


Example of an Array using For/In loop:

<html>
<head>
<title>for in loop in an array </title>
</head>
<body>
<script>
var array1 = new Array("html","css","javascript","php");
for(x in array1)
{
document.write(array1[x]+"<br>");
}
</script>
</body>
</html>

Output:

html
css
javascript
php


Example of an Array using For/Of loop:

<html>
<head>
<title>for of loop in an array </title>
</head>
<body>
<script>
var array1 = new Array(10,20,30,40,50);
for(x of array1)
{
document.write(x+"<br>");
}
</script>
</body>
</html>

Output:

10
20
30
40
50

JavaScript forEach() Method of An Array

The method which calls a function once for each element in an array, in order is called the forEach() method.

Example of an Array using forEach() method:

<html>
<head>
<title>forEach() method in an array </title>
</head>
<body>
<script>
res=0;
var num = [10,20,30,40,50];
num.forEach(sum);
document.write("Sum of array elements is "+res);

function sum(x)
{
res=(res+x);
}
</script>
</body>
</html>

Output:

Sum of array elements is 150