Exforsys

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction

Ads


Home arrow Technical Training arrow JavaScript Tutorial

JavaScript Arrays

Page 1 of 2
Author : Exforsys Inc.     Published on: 7th Jun 2007

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.

Ads

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”,….)


Ads

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”)




 
This tutorial is part of a JavaScript Tutorial tutorial series. Read it from the beginning and learn yourself.

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction
 

Comments