Exforsys.com
 
Home Tutorials JavaScript
 

JavaScript Arrays

 

Accessing JavaScript array elements

Page 2 of 2


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.




First Page: JavaScript Arrays


Read Next: JavaScript Functions



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape