Script with Errors Example
This is identical to the previous example, except the script has two errors. One is fatal, which causes the script to not execute. Once you fix that, the script will execute, but the formula for computing the area is not accurate. The formula error is a non-fatal error made by the programmer that simply results in an incorrect calculation.
Types of Errors:
-
Fatal Error - Script won't execute. Usually a syntax error.
Best defense is using JavaScript Error Console.
-
Logical Error - Script executes but does not work correctly. Programmer didn't implement correct solution to problem.
Best defense is figuring out how to solve the problem before you start to write code.
-
User Error - User enters bad input (like a non-number in this script), which usually results in bad output. Garbage In ... Garbage Out.
There are ways to validate user input, as a future lesson will show.
Fortunately, browsers have a built in JavaScript Console that will help you detect fatal errors. This example shows the Chrome browser's JavaScript Console, but all modern browsers have a similar utility.
If you don't use a JavaScript Console for the remainder of the JavaScript assignments, you are making a huge mistake.
Chrome Browser:
View → Developer → JavaScript Console
There are shortcut keys to make it easier to open the console.
Mac: Cmd + Option + J
Windows: Ctrl + Shift + J
When sitting down to work with JavaScript, popping the console should become a reflex action. It can save you lots of time since syntax errors can be hard for the human eye to detect.
Some screen shots are included below to show how the console shows the fatal error when this script loads.