JavaScript Array / PDF

JavaScript Array / PDF





JavaScript Array / PDF





Sample of the PDF document 






What is an Array? (reference only)


An important feature found in most programming and scripting languages is the use of an array. An array provides an indexed ordered list that a program uses to manipulate or display information. In simple terms, an array is "an ordered collection of data". The data in an array can be a number, a text string, a Boolean value or another array.

Arrays automatically assign a number to each entry in the array. These numbers like most numerical data in JavaScript start off with 0 and count up. The first item in the list will be referred to as 0 and the second item 1 and so on. These numbers are referred to as an Arrays index. In the example above, the first indexed item in the myCar array is chev.

Arrays are referenced by their variable name, like a function. By calling on the array and the index number, only that piece of data or literal string is returned. The format is variableName[#] with variableName being the name of the array and # being the index number of the listed entry. In the example above, myCar[0] would return the data Chev, while myCar[3] would return Lincoln.


Javascript creates several different arrays when a web page loads. For example, all the images in a web page are placed into an image array in the order they appear in the html code starting with the number 0. This image is referred to in the array as image[0].

You can find out how many images you have on a page by using the length attribute. The DOM format would be document.images.length. If you are using Firefox, visit the web page and then past the following into the url address field and hit enter:

javascript: document.images.length
In the example above, I visited the latimes website and pasted "javascript: document.images.length" in
the URL field and it return the number 121. At that time the page contained 121 images.

Array Syntax (reference only)


There are two different ways to declare an array in Javascript. One is shorter and easier to type while
the second is longer but easier to read and understand.
First format (dense array) :
var variableName = new Array("literal0","literal1", "literal2", "literal3")

Example:
var dayName = new Array("Sunday","Monday", Tuesday", "Wednesday", "Thursday", "Friday")


Second format:

var variableName = new Array(5)
variableName[0] = "literal0"
variableName[1] = "literal1"
variableName[2] = "literal2"
variableName[3] = "literal3"
variableName[4] = "literal4"

Example:
var dayName = new Array(7)
dayName[0] = "Sunday"
dayName[1] = "Monday"
dayName[2] = "Tuesday"
dayName[3] = "Wednesday"
dayName[4] = "Thursday"
dayName[5] = "Friday"
dayName[6] = "Saturday"...........




Download JavaScript Array / PDF





JavaScript Array / PDF




0 commentaires: