HTML and CSS Reference
In-Depth Information
In JavaScript, you add a semicolon to the end of each line to indicate that the statement is finished. A statement is a
command, such as calling a function or creating a variable.
You will be using variables extensively in the JavaScript programs you build throughout the remainder of this topic.
For example, in Chapter 11 you will be using variables to store references to buttons on your web page so that you
can build custom playback controls for a video.
Reserved Words
There are some words that are reserved for the JavaScript language; you should not use them as variable names. If
you try to do so, your program likely will not execute correctly.
Here is a list of all the reserved words for JavaScript. Have a quick read so that you are familiar with them.
break, case, catch, continue, debugger, default, delete, do, else,
finally, for, function, if, in, instanceof, new, return, switch,
this, throw, try, typeof, var, void, while, with
Null and Undefined
When you declare a variable, you do not have to initialize it with a value. You can simply declare a variable, as
shown here:
var age;
Variables that are declared but not initialized are known as undefined . Because no value is associated with them, the
browser cannot give them a type for you (for example, string or number).
Variables can also be null . This is similar to being undefined, but these do have a type. A null variable simply has
no value. It is empty, but it does have an implied type that was determined by its value in the past.
Let's write a little program to test the difference between undefined variables and null variables. Here are the
steps:
1. Create a new file in your text editor.
2. Save this file as example10-4.js .
3. Copy the following code into this file:
window.onload = function() {
// Undefined variable
var foo;
document.write('The foo variable is ' + foo + '<br>');
// Null variable
var bar = “Hello";
bar = null;
document.write('The bar variable is ' + bar);
};
4. Save the example10-4.js file.
5. Create a new HTML file called example10-4.html .
6. Add the following code to this new HTML file:
Search WWH ::




Custom Search