Page 143 - DCAP408_WEB_PROGRAMMING
P. 143
Unit 5: Scripting Language
<pre> Notes
<font size=”+2">
<script language=”JavaScript”>
<!— Hide script from old browsers.
document.write(“\t\tHello\nworld!\n”);
document.writeln(“\”Nice day, Mate.\”\n”);
document.writeln(‘Smiley face:<font size=”+3"> \u263a\n’);
//End hiding here. —>
</script>
</pre>
</body>
</html>
output
Hello
world!
“Nice day, Mate.”
Smiley face: :&
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). Since 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
hexidecimal character 233a is preceded by a \u.
The process of joining strings together is called concatenation. 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 following 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
LOVELY PROFESSIONAL UNIVERSITY 137