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."
For more information on these functions, see Chapter 9, "Built-in objects and functions."JavaScript provides several special functions for manipulating string and numeric values:
x = 42
var x = 42
For information on using variables across frames and windows, see Chapter 3, "Using windows and frames."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
.
'
) quotation marks. A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or double quotation marks. The following are examples of string literals:
"blah"
'blah'
"1234"
"one line \n another line"
Character |
Meaning
\b
|
backspace
|
\f
|
form feed
|
\n
|
new line
|
\r
|
carriage return
|
\t
|
tab
|
\\
|
backslash character
| |
---|
Escaping characters
For characters not listed in the preceding table, a preceding backslash is ignored, with the exception of a quotation mark and the backslash character itself.
You can insert quotation marks inside strings by preceding them with a backslash. This is known as escaping the quotation marks. For example,
var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service."
The result of this would be
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
document.write(quote)c:\temp
to a string, use the following:
var home =
"
c:\\temp"