Java Reference
In-Depth Information
You'll fi nd it very useful in this chapter if your browser is set up to show errors. You did this in
Chapter 2 in the section “Setting Up Your Browser for Errors.” So if you don't already have error
display set up, now would be a good time to do so.
Undefi ned Variables
JavaScript is actually very easygoing when it comes to defi ning your variables before assigning values
to them. For example, the following will implicitly create the new global variable abc and assign it to the
value 23:
abc = 23;
Although strictly speaking, you should defi ne the variable explicitly with the var keyword like this:
var abc = 23;
Whether or not you use the var keyword to declare a variable has a consequence of what scope the
variable has; so it is always best to use the var keyword. If a variable is used before it has been defi ned,
an error will arise. For example, the following code will cause the error shown in Figure 4-1 in IE8 if the
variable abc has not been previously defi ned (explicitly or implicitly):
alert(abc);
Figure 4-1
In Firefox you'll need to look in the JavaScript console, which you can view by choosing Tools
Error
Console.
In addition, you must remember that function defi nitions also have parameters, which if not declared
correctly can lead to the same type of error.
Take a look at the following code:
function foo(parametrOne)
{
alert(parameterOne);
}
Search WWH ::




Custom Search