Javascript Reference Card Page 4

ADVERTISEMENT

Initializing Arrays:
WHILE LOOP:
Array items can be treated as simple variables:
Format:
days[0] = “Sunday”;
while (condition) {
days[1] = “Monday”;
Statements;
etc.
Increment/decrement;
}
STRING OBJECT
Example:
Definition: String object is created by assigning a string to a
var i = 0;
variable, or by using the new object constructor.
while (i<=10)
{
Example:
document.write (“This is line “ + i);
i++;
var name = “Carol”;
}
var name = new String(“Carol”);
Hint: Watch out for infinite loops, which do not have a
Properties:
stopping condition or have a stopping condition that will
returns the number of characters in the
Length:
never be reached.
string
allows the user to add methods and
Prototype:
OBJECTS
properties to the string
Methods:
Definition: Objects are a composite data type which con-
String formatting methods (similar to HTML formatting tags)
tain properties and methods. JavaScript contains built-in
String.big
objects and allows the user to create custom objects.
String.blink
Creating Objects: Use the new constructor
String.italics
var X = new Array()
Substring methods (allow user to find, match, or change
Examples:
patterns of characters in the string)
date, time, math, strings, arrays
indexOf()
charAt()
ARRAY OBJECT
replace()
Definition: Array object is a variable that stores multiple val-
MATH OBJECT
ues. Each value is given an index number in the array and
Definition: Math object allows arithmetic calculations not
each value is referred to by the array name and the
supported by the basic math operators. Math is a built-in
index number. Arrays, like simple variables, can hold any
object that the user does not need to define.
kind of data. You can leave the size blank when you create
an array. The size of the array will be determined by the
Examples:
number of items placed in it.
returns absolute value of
Math.abs(number)
Format:
the numeric argument
var arrayname = new Array(size)
returns the cosine of the
Math.cos(number)
argument, in radians
Hint: When you create an array, you create a new instance
of the array object. All properties and methods of the array
rounds number to the
Math.round(number)
object are available to your new array.
nearest integer
Example:
DATE/TIME OBJECTS
var days = new Array (7)
Date object provides methods for getting or setting infor-
This creates an array of seven elements using the array
mation about the date and time.
constructor.
Note: Dates before January 1, 1970 are not supported.
The first item is days[0], the last item is days[6].
4

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 6