Quick Reach
- 1 The arrays in JavaScript
- 2 A few quick online array examples
- 3 How to define arrays?
- 4 Accessing array elements
- 5 Example to declare, initialize and accessing JavaScript arrays
- 6 JavaScript iterate array example with For loop
- 7 JavaScript string array example
- 8 An array example with different objects
- 9 JavaScript Associative array
- 10 JavaScript multidimensional array
- 11 Useful Array methods
- 12 Related
The arrays in JavaScript
The array is a type of variable that is used to store multiple values. The JavaScript arrays can hold multiple data type values in a single array. For example, an array can contain numbers and strings:
var jsarray = [“string 1”, “string 2”, 1, 2];
An example of declaring and accessing arrays
Arrays are quite useful and especially used in the case where you have to create multiple variables for the same type of data. For example, to save US 52 states names you have to create 52 different variables:
Var state1 = “New York”;
Var State 2 = “Florida” ;
Var state 3 = “Texas”;
And so on.
Whereas by using a single array variable you can save all 52 state names in a single array as follows:
var USStates = [“New York”, “Texas”, “Florida”, “California”];
Is not that simple and useful?
A few main points about JavaScript arrays are:
- The arrays are a special type of variables.
- Arrays can store multiple values.
- The array elements can be of different types like numbers, strings etc.
- The array JavaScript index starts at 0.
- An array is basically an object.
- JavaScript has built-in functions to work with arrays e.g. sort, length etc. explained in their separate chapters. (see the links below)
A few quick online array examples
A basic array example with numbers
An array iteration example with For loop
JavaScript string array example
An array example with different datatypes and objects
While this chapter explains what are arrays, you can find online examples to see arrays with code. But let us first look at how to create or define an array.
How to define arrays?
Following is the create array syntax:
var jsArr = [element1, element2, element3…..];
e.g.
var USStates = [“New York”, “Texas”, “Florida”, “California”];
As you can see in above syntax, var keyword is followed by an array name and then array elements on the right side in brackets. Each array element is separated by a comma.
This is the one way of declaring an array. You can declare arrays by using the new keyword as well. The syntax is shown below.
Create arrays with new keyword
The syntax to create an array by the new keyword is:
var jsArr = new array(“element1, element2, …..”);
e.g.
var USStates = new array (“New York”, “Texas”, “Florida”, “California”);
That is array name on the left side with the var keyword. While right side contains new keyword which is followed by an array and the array elements in parenthesis.
Although, there is no difference in creating arrays by both methods, however, the first method is simple to use and faster in execution.
Accessing array elements
As mentioned earlier, the array index starts at zero. After creating an array. the elements are accessed by using the index values.
For example, consider this array:
var USStates = [“New York”, “Texas”, “Florida”, “California”];
The USStates array has four elements. This is how the element sequence by index number is:
The USstates[0] contains “Ney York” value,
The USstates[1] contains “Texas” value and so on.
See the following online examples of declaring, initializing and accessing arrays.
Example to declare, initialize and accessing JavaScript arrays
Following is a very basic array example. We have created a simple numeric array of five elements. As you click on the link below the demo page loads and an alert will show the 0 index value of NumArr array.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!DOCTYPE html>
<html>
<head>
<script type=“text/javascript”>
var NumArr = [1,2,3,4,5];
alert (NumArr[0]);
</script>
</head>
<body>
</body>
</html>
|
As you can see the array element is accessed by using NumArr[0] in an alert.
JavaScript iterate array example with For loop
The Following example shows how to iterate a JavaScript array by using the for loop. The For loop allows going through each array element where you can use array’s length method that returns array count.
The example declares an array with five elements that is numbers. After that, the for loop is used to loop through the array.
The length property is used in the for loop to know the length of the array. After that, the elements are displayed in HTML paragraph after assigning to a variable. See the demo by clicking the link or image along with JS code.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id=“p1”></p>
<script type=“text/javascript”>
var NumArr = [1,2,3,4,5];
var i;
var arrdisplay = “The array elements are: <BR>”;
for (i=0; i < NumArr.length; i++){
arrdisplay += NumArr[i] + “<BR>”;
}
document.getElementById(“p1”).innerHTML = arrdisplay;
</script>
</body>
</html>
|
JavaScript string array example
In the above examples, we used only numbers in arrays. You can use strings in JS arrays as well. Following example creates an array, USStates with four State names.
After that, the for loop is used to loop through array elements and finally, we will print the array of State names in an HTML paragraph.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id=“p1”></p>
<script type=“text/javascript”>
var USStates = [“New York”, “Texas”, “Florida”, “California”];
var i;
var arrdisplay = “The State names in array are: <BR>”;
for (i=0; i < USStates.length; i++){
arrdisplay += USStates[i] + “<BR>”;
}
document.getElementById(“p1”).innerHTML = arrdisplay;
</script>
</body>
</html>
|
You can see, we created an array and then used length property to return array count in the for loop to iterate through the array. Finally, we printed the state names in HTML paragraph.
An array example with different objects
The above examples show using numbers and strings in the arrays. Arrays not only stores number or strings, but it can be used to store objects as well. You can even store arrays in an array. The array elements can also store functions as well.
The array is so flexible that a single array can contain numbers, strings, functions, arrays etc.
The following example is just an illustration of using all these elements in an array. We created an array Allarray, that is assigned a number, a string, a function, another array and a date object.
Then we used a for loop, that will show an alert for each Allarray element. See yourself by clicking the link or image below:
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<!DOCTYPE html>
<html>
<head>
<script type=“text/javascript”>
function AFunction() {
alert (“A simple function”);
}
var Anotherarray = [5,10,15];
var currdate = Date.now();
var Allarray = [1, “A string”, AFunction, Anotherarray,currdate];
for (i=0; i < Allarray.length; i++){
alert (Allarray[i]);
}
</script>
</head>
<body>
</body>
</html>
|
JavaScript Associative array
Programming languages like Java, PHP etc. support associative arrays. The associative arrays are named index rather numbers that we learned in the above section. In the associative arrays, the array elements with index may look like this:
JSArray[“EmployeeName”] = “Mike”;
JSArray[“EmployeeAge”] = 35;
JSArray[“EmployeeSalary”] = 3500;
However, there is no JavaScript associative array. Instead of an associative array, you can use objects.
JavaScript multidimensional array
Though JavaScript does not provide a specific method to create multidimensional arrays. You can also create a multidimensional array in JavaScript. Generally, the multidimensional array used are 2d and 3d.
Following is a basic way to create JavaScript 2d array.
Creating JavaScript two-dimensional array
This is how you can create a 2d array:
var js2darry = [[],[]];
See the following example of creating a multidimensional array.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<!DOCTYPE html>
<html>
<head>
<script type=“text/javascript”>
var i;
var j;
var js2darry = [[],[]];
js2darry[0,0] = 100;
js2darry[0,1] = 200;
js2darry[0,2] = 300;
for (i=0; i<1; i++){
for (j=0; j<=2; j++){
alert (“Current element value: “ + js2darry[i,j]);
}
}
</script>
</head>
<body>
</body>
</html>
|
Useful Array methods
Following are useful array functions and property with links to their respective chapters.
- array push
- array length (property)
- array sort
- array splice
Click on any array’s function to go to its chapter with examples.
Further reading: JavaScript Strings
Leave A Comment?