Technical Training
JavaScript TutorialJavaScript Arrays
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”) |
JavaScript Tutorial
- JavaScript Browser Objects Part 2
- JavaScript Frame object
- JavaScript Form Object
- JavaScript FileUpload Object
- JavaScript Event Object Properties and Methods
- JavaScript Event Object
- JavaScript Elements and Embed Objects
- JavaScript Applet Objects
- JavaScript Browser Objects
- JavaScript Object Oriented Features
- JavaScript Window Object Open Method Part 2
- JavaScript Window Object Open Method
- JavaScript Window Object Timeout Methods
- JavaScript Location Object
- JavaScript Location Object Properties
- JavaScript History Object Properties and Methods
- JavaScript Document Object Methods Part II
- JavaScript Document Object Methods Part I
- JavaScript Document Object Properties
- JavaScript Document Object
- JavaScript Windows Object Properties Part II
- JavaScript Windows Object Properties Part I
- JavaScript DOM Window Object
- Working with JavaScript DOM Objects
- JavaScript Array Object Methods – Part II
- JavaScript Array Object
- JavaScript Array Object Methods – Part I
- JavaScript Boolean Object
- JavaScript OnError Event
- JavaScript Exception Handling – Part II
- JavaScript Exception Handling – Part I
- JavaScript Event Handler
- JavaScript Events Handling
- JavaScript Array Operations
- JavaScript Two Dimensional Arrays
- Passing values to JavaScript Function
- JavaScript Functions
- JavaScript Arrays
- JavaScript Iterative Structures - Part II
- JavaScript Iterative Structures - Part I
- JavaScript Math Object
- JavaScript Date Object
- JavaScript String Object
- JavaScript Objects
- JavaScript Confirm Box
- JavaScript Alert Box
- JavaScript Conditional Statements Part 2
- JavaScript Conditional Statements Part 1
- How to use JavaScript in HTML page
- JavaScript Variables
- JavaScript Features
- JavaScript Introduction







