Content Menu

Home-What is JavaScript?

JavaScript tutorials

1. JavaScript Basics

2. JS Simple Coding

3. JS InterActive Content

4. Images and JS

5. JS Functions

6. JavaScript Events

7. JS Conditional

8. JS Loops

9. Other

Javascript Array

Array is a special kind of variable that can hold multiple values. If you recall the lesson on JS variables, we explained that variable is a container of a value.

Lets use analogy of a box. A simple variable is a box that holds one item like box containing a large chocolate cake. Now imagine array as a box that has many subdivisions and each compartment has a pastry of a different flavor.

Practically Array is many variables within one.

Why do we need Javascript Arrays / Advantages of using Arrays

Syntax Define / Create Array- Method 1

First create an array, then assign values to its individual indexes.

var arrayname = new Array ();
arrayname[0] = "value";
arrayname[1] = "value";
arrayname[2] = "value";
arrayname[3] = "value"; And so on

Notice that the subcontainer IDs start from 0

Example

js array

Notice how we referred to the 3rd index the array using the document.write command to display it on browser screen.

Syntax Create Array- Method 2

Create an array and assign value in the same statement / command

var arrayname = new Array("value", "value", "value");

Example

javascript define array example

Both Methods are valid. Use the one you find easier and faster.