The notes found on these pages all reference the textbook given above. Unless otherwise stated, the citation of the information on these web pages is that text.
There are many built-in objects and arrays in Javascript. Each object includes numerous methods. You should have seen date and timeout, but here are samples for you:
The string object has many methods/functions for you to use. Although it is not usually necessary, you can create a String object with the new operator
Arrays revisited. Users can declare arrays or use builtin arrays
Code | Description | Output |
var sandwiches = new Array("turkey", "chicken", "bacon"); | Create an array of 3 elements | |
document.write (sandwiches[1]); | Display second element | |
if (1 in sandwiches) | Check for second element | |
document.write ("<br>there"); | found it |
Code | Description | Output |
var names = new Array(3); | Create an array of 3 elements | |
names["first"] = "Sue" | set element named first to Sue | |
names["second"] = "Dan" | set element named second to Dan | |
names["third"] = "Joe" | set element named third to Joe | |
document.write (names["first"]); | display element named "first" |
To create 2D arrays, you need an array where each element of the array is an array (think table where each row of a table is an array)
This can be done via a var statement or via a loop in Javascript.
A 4 x 5 example:
var twoDimArray = [new Array(5), new Array(5), new Array(5), new Array(5)]Or:
var twoDimArray = new Array(4); for (i=0; i < 4; i++) twoDimArray[i]=new Array(5);