Errors
Four kinds of error
Everybody writes broken programs. What separates a five-minute bug from a five-hour one is which kind of mistake it is, and that comes down to a single question: when does the trouble show up?There are four general kinds: syntax errors, run-time errors, overflow errors and logic errors.
Nothing runs until the whole file is read
JavaScript does not begin carrying out your instructions the moment you press run. It reads the entire file first and works out what the instructions are. If it meets something that is not legal JavaScript, it stops there and reports it, and not one line of the program runs, not even the lines above the mistake.Syntax errors
A syntax error is a misuse of the language itself. A quote with no partner, a bracket that never closes, a value being stored somewhere that is not a name. JavaScript reports these as a SyntaxError, and it reports them before anything happens.Run-time errors
A file can be legal JavaScript from top to bottom and still fall over. Some mistakes only surface once real values are moving through the program, and those are run-time errors. We say JavaScript throws one.A thrown error stops the program dead at that line. Whatever printed before it stays on the screen. Nothing below it happens at all.
Two run-time errors turn up far more than any others, and their names are worth learning.
• ReferenceError: you used a name that was never declared.
• TypeError: you asked a value to do something that kind of value cannot do.
Overflow
An overflow error happens when a value grows past the largest number the space set aside for it can hold. In most languages that is a run-time error and the program stops.JavaScript does not stop. Every number it stores has a ceiling of about 1.8e308, and any result above that ceiling quietly becomes Infinity. Nothing is thrown. Nothing is reported. The program carries on holding a value that is no longer a number in any useful sense.
The same fixed space gives the range a floor as well as a ceiling. A result that is closer to zero than the smallest amount JavaScript can represent does not become Infinity; it quietly becomes 0. That end of the range is the harder one to catch, because Infinity at least looks wrong on the screen and a zero looks like an ordinary answer somebody meant to compute.
The silence is the whole problem. An overflow in JavaScript never announces itself, and Infinity plus anything is still Infinity, so a value that stopped meaning anything back on line 4 travels quietly all the way to the end of the program and gets printed as an answer.
Logic errors
JavaScript catches syntax errors while it reads and throws run-time errors while it runs. Those two are the easiest kind of bug there is to fix, for one reason: you are handed the address. The error names the kind of problem and the line it happened on, and you go there.A logic error is the other thing entirely. The program is legal. It runs from beginning to end. It reports nothing at all. And the answer it gives is wrong. No line is pointed at, because no rule was broken.
| Kind of error | Does it start? | Does it finish? | Who tells you? |
|---|---|---|---|
| Syntax error | No | No | JavaScript, before any line runs |
| Run-time error | Yes | No, it stops part-way | JavaScript, at the line that failed |
| Overflow | Yes | Yes | Nobody. You get Infinity |
| Logic error | Yes | Yes | Nobody. You have to notice |
The program below prints a trip report and works out an average speed, and it runs with no errors. Run it, then make each change below and run it again. Press Reset before each change, so that each one starts from the original program. Each time, notice whether anything printed, whether the last line ran, and whether a message arrived.
• Delete the
= in let hours = 4;.• Change
distance / hours to distance / hour.• Set
hours to 0.console.log("Trip report");
let distance = 150;
let hours = 4;
let speed = distance / hours;
console.log("Average speed:", speed);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.