Java Reference
In-Depth Information
gone wrong, they often do so in an oblique way. In this case the browser thinks you are either trying
to use an object called If or use an undefined function called If .
Note Different browsers use different wording when displaying errors. The
overall meaning is the same, however, so you can identify what the problem is.
Okay, with that error cleared up, you come to the next error, not one of JavaScript syntax, but a
logic error. Remember that Jeremy does not equal jeremy in JavaScript, so myName == "jeremy" is
false , even though it's quite likely that you didn't care whether the word is Jeremy or jeremy . This
type of error will result in no error message at all because it is valid JavaScript; your only clue is that
your code will not execute as you'd planned.
The third fault is with the toUpperCase() method of the String object. The
previous code uses toUppercase , with the C in lowercase. IE gives us the message
Object doesn't support this property or method and Firefox reports that
myName.toUppercase is not a function . On first glance it would be easy to miss such
a small mistake and start checking your JavaScript reference guide for that method. You might
wonder why it's there, but your code is not working. Again, you always need to be aware of case,
something that even experts get wrong from time to time.
incorrect number of Closing Braces
In the following code, you define a function and then call it. However, there's a deliberate mistake.
See if you can spot where it is:
function myFunction()
{
var x = 1;
var y = 2;
if (x <= y)
{
if (x == y)
{
alert(“x equals y”);
}
}
myFunction();
This is why formatting your code is important—you'll have a much easier time spotting errors such as this:
function myFunction() {
var x = 1;
var y = 2;
if (x <= y) {
if (x == y) {
alert("x equals y");
}
}
myFunction();
 
Search WWH ::




Custom Search