Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Q&A

Q1:

Why does Netscape display the error message "missing semicolon before statement" when I try the function example?

A1:

This is probably because you mistyped something. JavaScript is case-sensitive, so be sure you get the capitalization right. This specific error message will appear if you use the keyword Function instead of function.

Q2:

What is the importance of the var keyword? Should I always use it to declare variables?

A2:

You only need to use var to define a local variable in a function. However, if you're unsure at all, it's always safe to use var. Using it consistently will help you keep your scripts organized and error-free.

Q3:

Is there any reason I would want to use the var keyword to create a local variable with the same name as a global one?

A3:

Not on purpose. The main reason to use var is to avoid conflicts with global variables you may not know about. For example, you may add a global variable in the future, or you may add another script to the page that uses a similar variable name. This is more of an issue with large, complex scripts.

Q4:

What good are boolean variables?

A4:

Often in scripts you'll need a variable to indicate whether something has happened—for example, whether a phone number the user has entered is in the right format. Boolean variables are ideal for this; they're also useful in working with conditions, as you'll see in Hour 6.

Share ThisShare This

Informit Network