alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow JavaScript arrow JavaScript Arrays
Site Search


JavaScript Arrays
Article Index
JavaScript Arrays
Accessing JavaScript array elements

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:


var Exforsys=new Array( )

Exforsys[0]=”
Training
Exforsys[1]=”
Division
Exforsys[2]=”
Institute
Exforsys[3]=”
Company

document.write(exforsys[2])

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.


Trackback(0)
Comments (0)add comment

Write comment

busy

 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape