Tutorial: JavaScript:Defining Array

Array in JavaScript is class derived from Object. There are several ways of defining an array:

1. Regular array. Pass an optional integer argument to control array's size. However, if it is defined, no more element can be defined exceeding this number.
; var myfriends = new Array(optionalElementNumber)
; myfriends[0] = "John"
; myfriends[1] = "Bob"
; myfriends[2] = "Sue"

2. Condensed array:
; var myfriends = new Array("John", "Bob", "Sue")

3. Literal array:
; var myfriends = ["John", "Bob", "Sue"]

4. Empty array:
; var myArray = []
; myArray.push("John")
; myArray.push("Bob")
; myArray.push("Sue")

The elements/values of an array can be numeric, string, another array and object, or reference points to these:
; var var_0 = "John"
; var var_1 = "Bob"
; var var_2 = "Sue"
; var myfriends = new Array(var_0, var_1, var_2)
or
; var myfriends = [var_0, var_1, var_2]

When an element is another array, it becomes multidimensional array.

No comments:

Post a Comment

Labels