null
, a special keyword denoting a null valueNOTE: Because JavaScript is case sensitive,This relatively small set of types of values, or data types, enables you to perform useful functions with your applications. There is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type in Navigator. However, you can use thenull
is not the same asNull
,NULL
, or any other variant.
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 loosely typed, this assignment does not cause an error message.
In expressions involving numeric and string values, JavaScript converts the numeric values to strings. For example, consider the following statements:
x = "The answer is " + 42The first statement returns the string "The answer is 42." The second statement returns the string "42 is the answer."
y = 42 + " is the answer."
A JavaScript identifier, or name, must start with a letter or underscore ("_"); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
Some examples of legal names are Number_hits
, temp99
, and _name
.
Using var
to declare a global variable is optional. However, you must use var
to declare a variable inside a function.
For information on using variables across frames and windows, see Chapter 4, "Using Windows and Frames."
Some examples of integer literals are: 42 0xFFF, and -345.
Some examples of floating-point literals are 3.1415, -3.1E12, .1e12, and 2E-12
true
and false
."
) 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:"blah"
'blah'
"1234"
"one line \n another line"
Table 8.1 JavaScript special characters
Character | Meaning |
---|---|
\b | backspace |
\f | form feed |
\n | new line |
\r | carriage return |
\t | tab |
\\ | backslash character |
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 path
c:\temp
to a string, use the following:var home ="
c:\\temp"
Last Updated: 11/26/97 09:25:48