JavaScript Arrays
In this JavaScript tutorial, you will learn about JavaScript Array, different ways of defining an array in JavaScript viz. traditional way, shorter form, literal array, accessing the elements in an array and editing the values or elements in an array.
The array concept is used to store a set of values in a single variable name. This is a very important concept in any programming language. In this section, how to define and handle arrays in JavaScript is examined.
In order to work with the concept of arrays in JavaScript, there are many sections the programmer must become familiar with.
- Array Definition
- Accessing the elements in an array
- Editing the values or elements in an array.
- Single Dimensional Array
- Two Dimensional Arrays
- Different Operations with Array
Array Definition:
The array must first be defined before it is accessed or used. There are a variety of ways this can be accomplished in JavaScript. Below are some of the ways of defining arrays in JavaScript. Depending on convenience, the programmer can choose from any of the following techniques to define arrays in JavaScript:
Defining an Array in JavaScript:
Array is defined in JavaScript by making use of the keyword new.
General format of defining array in JavaScript is as follows:
var varaiblename = new Array( )
For example, if a programmer wants to define an array exforsys, it is written as follows:
var exforsys = new Array( )
Values or elements can be added in various ways to the above-defined array:
Traditional way:
This is the traditional way of adding elements to array in JavaScript. The first element of the array in JavaScript always starts with an index as zero.
The format for adding elements in this structure is as follows:
var varaiblename = new Array( )
variablename[0]=”element1”
variablename[1]=”element2”
variablename[2]=”element3”
variablename[3]=”element4”
………………………
………………………
For example, if the programmer wants to store 4 elements (“Training”, “Division”, “Institute”, “Company” ) to the array named Exforsys, it is written as follows:
var Exforsys = new Array( )
Exforsys[0]=”Training”
Exforsys[1]=”Division”
Exforsys[2]=”Institute”
Exforsys[3]=”Company”
It is also possible that if the programmer is certain of the array length, it can be defined while defining the array itself as:
var Exforsys = new Array(4)
Note that in the above example, since the value of the elements are strings, they are defined inside the double quotes. If the elements to be stored are integers, then they are placed without the double quotes. For example, if one wants to store 15 in array Exforsys it is written as follows:
var Exforsys = new Array( )
Exforsys[0]=15
Shorter form:
The elements can also be defined in a shorter form as:
var variablename=new Array(“element1”,”element2”,”elements3”,….) |
The same example used above (the array named Exforsys containing 4 elements) can be represented in a shorter form as: By this method of representation, the definition of the array and the defining of the elements in the array is written in a single step.
var Exforsys=new Array(”Training”,”Division”,”Institute”,”Company”) |
Accessing the elements in an array:
An element in an array can be accessed by referring to the name of the array, followed by open braces [inside which the index number of the element to be accessed is placed and then the braces are closed using].
The index of an array in JavaScript starts with 0.
For example,
if a programmer wants to write the element “Institute” define in the array exforsys below.
It is written as follows:
document.write(exforsys[2])
var Exforsys=new Array( )
Exforsys[0]=”Training”
Exforsys[1]=”Division”
Exforsys[2]=”Institute”
Exforsys[3]=”Company”
will write Institute as output.
Editing the values or elements in an array:
It is also possible that the value or elements of the array defined can be edited or modified.
This is written using the same approach for accessing array elements.
For example,
if a programmer wants to change the value of “Division” to “Output” it can simply be written as follows:
Exforsys[1]=”Output”
Literal Array:
General format for defining the literal array is as follows:
var variablename=[“element1”,”element2”,…….]
For example, if a programmer wants to define an array named exforsys with the first element “Example”, the second and third elements of the array as undefined and the fourth element as “Training”, it can be written as follows:
var Exforsys=[“Example”, , , “Training”]
Thus, using this method, the programmer has the flexibility to leave elements undefined between the arrays.