Common Errors
What follows are examples of the most common errors that newcomers to JavaScript make. They are presented to you so you will be aware of these kinds of errors and can avoid them as you write your code. They aren't presented in any sort of technical order and aren't a complete list by far, but if you can learn to identify these, your JavaScript projects will have a much better chance of working the first time.
Typos and Spellos!
Most of the errors you'll encounter when writing JavaScript (if you follow the examples laid down here) will probably fall into these categories. Typos occur when you accidentally hit the wrong key or combination of keys while typing.
Examples of typos are such things as
Pressing the wrong key:
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!--Cloaking device on! function myFunction() { //Hitting the wrong key when closing the parentheses below alert("Hello there"_; } //Cloaking device off --> </script> </head> <body onLoad="myFunction()"> </body> </html>
Forgetting to press Shift, such as before the curly bracket:
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!--Cloaking device on! function myFunction() //Forgetting to press Shift before the curly bracket [ alert("Hello there"); } //Cloaking device off --> </script> </head> <body onLoad="myFunction()"> </body> </html>
hAVING cAPS lOCK oN:
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!--Cloaking device on! //Having Caps Lock on! FUNCTION MYFUNCTION() { alert("Hello there"); } //Cloaking device off --> </script> </head> <body onLoad="myFunction()"> </body> </html>
Typing in the wrong mathematical operator (especially when using the numeric keypad):
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!--Cloaking device on! function myFunction() { //Concatenation operator wrong! alert("Hello " - "there"); } //Cloaking device off --> </script> </head> <body onLoad="myFunction()"> </body> </html>
Spellos are different from typos because spellos occur when you have spelled something incorrectly that perhaps you typed in correctly earlier.
Examples include the following:
Built-in methods and functions:
<html> <head> <title>A Simple Page</title> <script language="JavaScript "> <!--Cloaking device on! function myFunction() { //That 's not how alert is supposed to be spelled alerg("Hello there "); } //Cloaking device off --> </script> </head> <body onLoad="myFunction()"> </body> </html>
Variables:
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!--Cloaking device on! function myFunction() { var msg ="Hello there"; //Wrong variable name alert(mssg); } //Cloaking device off --> </script> </head> <body onLoad="myFunction()"> </body> </html>
Another that can occur is using the wrong bracket. Remember, JavaScript uses three types:
()
{}
[]
And they are not interchangeableyou must use the correct one:
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!--Cloaking device on! function myFunction() //Wrong bracket ( var msg ="Hello there" alert(msg); //...and here ) //Cloaking device off --> </script> </head> <body onLoad="myFunction()"> </body> </html>
These are just a few examples of errors that beginners frequently make (as well as those who are not beginners!). Recognize them and your JavaScript will be a lot less buggy!