Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

476 LESSON 17: Introducing JavaScript


happens to be valid JavaScript, and the Console automatically evaluates it and prints the
results for you. Now try this:
alert(1 + 1);
You can see what the results look like in Figure 17.2.

When you enter that line, an alert dialog will pop up containing the number 2. As you
can see, you can affect the browser environment using the Console. The word undefined
is printed in the Console. alert, which I’ll discuss further later, opens the dialog box.
It is also an expression, just like 1 + 1, and the results of evaluating that expression are
printed in the Console window. In this case, the expression’s result is undefined, which
is why that’s printed. That’s what any expression that does not return a useful value
returns in JavaScript. If you’re not already a programmer, you may be wondering what an
expression is at all. I’ll get into that a bit later as well.

The JavaScript Language


When JavaScript is included in a page, the browser executes that JavaScript as soon as it
is read. Here’s a simple page that includes a script; the output is included in Figure 17.3 :

Input ▼
<!DOCTYPE html>
<html>
<head>
<title>A Simple JavaScript Example</title>
</head>
<body>

<p>Printed before JavaScript is run.</p>

<p>
<script>
document.write("Printed by JavaScript.");

FIGURE 17.2
Expressions evalu-
ated using the
Console.
Free download pdf