true
or false
.null
, a special keyword denoting a null value; null
is also a primitive value. Because JavaScript is case sensitive, null
is not the same as Null
, NULL
, or any other variant.undefined
, a top-level property whose value is undefined; undefined
is also a primitive value.Date
object and its methods to handle dates.
Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.
var answer = 42And later, you could assign the same variable a string value, for example,
answer = "Thanks for all the fish..."Because JavaScript is dynamically typed, this assignment does not cause an error message. In expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings. For example, consider the following statements:
x = "The answer is " + 42 // returns "The answer is 42"In statements involving other operators, JavaScript does not convert numeric values to strings. For example:
y = 42 + " is the answer" // returns "42 is the answer"
"37" - 7 // returns 30
"37" + 7 // returns 377
Number_hits
, temp99
, and _name
.
undefined
. The result of evaluating an unassigned variable depends on how it was declared:
function f1() {
return y - 2;
}
f1() //Causes runtime error
function f2() {You can use
return var y - 2;
}
f2() //returns NaN
undefined
to determine whether a variable has a value. In the following code, the variable input
is not assigned a value, and the if
statement evaluates to true
.
var input;The
if(input === undefined){
doThis();
} else {doThat();
}
undefined
value behaves as false when used as a Boolean value. For example, the following code executes the function myFunction
because the array element is not defined:
myArray=new Array()When you evaluate a null variable, the null value behaves as 0 in numeric contexts and as false in Boolean contexts. For example:
if (!myArray[0])
myFunction()
var n = null
n * 32 //returns 0
var
to declare a global variable is optional. However, you must use var
to declare a variable inside a function.
You can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called phoneNumber
is declared in a FRAMESET
document, you can refer to this variable from a child frame as parent.phoneNumber
.
coffees
array with three elements and a length of three:
coffees = ["French Roast", "Columbian", "Kona"]
NOTE: An array literal is a type of object initializer. See "Using Object Initializers" on page 139.If an array is created using a literal in a top-level script, JavaScript interprets the array each time it evaluates the expression containing the array literal. In addition, a literal used in a function is created each time the function is called. Array literals are also
Array
objects. See "Array Object" on page 145 for details on Array
objects.
fish
array:
fish = ["Lion", , "Angel"]This array has two elements with values and one empty element (
fish[0]
is "Lion", fish[1]
is undefined, and fish[2]
is "Angel"):
If you include a trailing comma at the end of the list of elements, the comma is ignored. In the following example, the length of the array is three. There is no myList[3]
. All other commas in the list indicate a new element.
myList = ['home', , 'school', ];In the following example, the length of the array is four, and
myList[0]
is missing.
myList = [ , 'home', , 'school'];In the following example, the length of the array is four, and
myList[3]
is missing. Only the last comma is ignored. This trailing comma is optional.
myList = ['home', , 'school', , ];
true
and false
.
Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object. The Boolean object is a wrapper around the primitive Boolean data type. See "Boolean Object" on page 148 for more information.
car
object defines a property, myCar
; the second element, the getCar
property, invokes a function (Cars("honda")
); the third element, the special
property, uses an existing variable (Sales
).
var Sales = "Toyota";
function CarTypes(name) {
if(name == "Honda")
return name;
else
return "Sorry, we don't sell " + name + ".";
}
car = {myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales}
document.write(car.myCar); // SaturnAdditionally, you can use an index for the object, the
document.write(car.getCar); // Honda
document.write(car.special); // Toyota
index
property (for example, 7), or nest an object inside another. The following example uses these options. These features, however, may not be supported by other ECMA-compliant browsers.
car = {manyCars: {a: "Saab", b: "Jeep"}, 7: "Mazda"}
document.write(car.manyCars.b); // Jeep
document.write(car[7]); // Mazda
"
) or single ('
) quotation marks. A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or both double quotation marks. The following are examples of string literals:
You can call any of the methods of the String object on a string literal value--JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the String.length
property with a string literal.
You should use string literals unless you specifically need to use a String object. See "String Object" on page 155 for details on String
objects.
"one line \n another line"The following table lists the special characters that you can use in JavaScript strings.
Table 4.1 JavaScript special characters
Character |
Meaning
\b
| \f
| \n
| \r
| \t
| \'
| \"
| \\
| \XXX
| \xXX
| |
---|
var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service."The result of this would be
document.write(quote)
He read "The Cremation of Sam McGee" by R.W. Service.
To include a literal backslash inside a string, you must escape the backslash character. For example, to assign the file pathc:\temp
to a string, use the following:
var home ="
c:\\temp"
Last Updated: 11/12/98 15:29:12