HTML and CSS Reference
In-Depth Information
EXPLANATION
1
The escape sequences will work only if in a
<pre>
tag or an alert dialog box.
2
The JavaScript program starts here.
3
The
write()
method sends to the browser a string containing two tabs (\
t\t
),
Hello
,
a newline (
\n
)
, world!,
and another newline (
\n
).
4
The
writeln()
method sends to the browser a string containing a double quote (
\”
),
Nice day, Mate.
, another double quote (
\”
), and a newline (
\n
). Because the
writeln()
method automatically creates a newline, the output will display two
newlines: the default value and the
\n
in the string.
5
This string contains a backslash sequence that will be translated into Unicode.
The Unicode hexadecimal character 263a is preceded by a
\u
. The output is a smi-
ley face. See Figure 3.1.
Figure 3.1
Escape sequences.
Putting Strings Together.
The process of joining strings together is called concate-
nation. The string concatenation operator is a plus sign (+). Its operands are two strings.
If one string is a number and the other is a string, JavaScript will still concatenate them
as strings. If both operands are numbers, the + will be the addition operator. The follow-
ing examples output
“popcorn”
and
“Route 66”
, respectively.
document.write("pop" + "corn");
document.write("Route " + 66);
The expression
5 + 100
results in
105
, whereas “
5
”
+ 100
results in “
5100
”.
Boolean Literals.
Boolean literals are logical values that have only one of two values,
true
or
false
. You can think of the values as yes or no, on or off, or 1 or 0. They are used
to test whether a condition is true or false. When using numeric comparison and equal-
ity operators, the value
true
evaluates to 1 and
false
evaluates to 0. (Read about compar-
ison operators in Chapter 5, “Operators.”)