Syntax vs Runtime Errors
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Errors Are Normal
Every programmer runs into errors every single day. Seeing a red error message does not mean you failed β it means JavaScript is giving you a clue about what needs to be fixed! To fix errors quickly, you first need to know the difference between the main kinds of errors. Here is the first kind:
console.log("Battle start!");
const damage = ; // syntax error!
console.log("Zap attacks!");Run the code above and look closely: nothing prints to the console β not even `"Battle start!"` on line 1! This is a syntax error (`SyntaxError`). Before running your code, JavaScript reads the entire file to check its grammar. If a statement is incomplete or a parenthesis is missing, JavaScript cannot even start running the program.
Exercise 2 β Runtime Errors
Now compare that with a runtime error. Here, every parenthesis, quote, and semicolon is in the right place, so the grammar is completely valid:
console.log("Battle start!");
const team = ["Zap"];
console.log(team[5].name); // runtime error!
console.log("Zap attacks!");Run the code above. This time, `"Battle start!"` does print before the program crashes on line 4! 1. Syntax error β Broken grammar (like `const damage = ;`). Checked before the code runs, so nothing executes at all. 2. Runtime error β Valid grammar, but an impossible action while running (like reading `.name` on `team[5]`, which is `undefined`). Every line before the crash still executes.
Exercise 3 β Why the Difference Matters
Knowing whether you have a syntax error or a runtime error tells you where to look for the problem: 1. Syntax error β Look for a typo in the code itself: an unclosed string, a missing bracket `}`, or a missing parenthesis `)`. 2. Runtime error β Your code grammar is fine, but a variable holds an unexpected value when that line runs (such as `undefined` instead of a string or object).
Exercise 4 β Quiz: Which Kind?
This program prints `"start"` and then crashes. Which kind of error is it?
Hint : Ask whether anything ran before the crash.
Exercise 5 β Quiz: Nothing Printed
This program has a `console.log` on its first line, yet nothing is printed at all. Which kind of error is it?
Hint : One kind of error stops the program before the very first instruction.
Exercise 6 β Quiz: Spot the Syntax Error
Which line is a syntax error?
const hp = ;const hp = team[9].level;const hp = undefined;const hp = 0 / 0;Hint : Three of these are valid JavaScript that may or may not work at runtime. One can't even be read.
Exercise 7 β The Silent Bug: Logic Errors
There is a third kind of problem that is even sneakier: a logic error (often called a bug). With a logic error, the grammar is valid and no line crashes β so JavaScript never shows an error message! The program runs all the way to the end, but produces the wrong result:
const hp = 50;
const damage = 20;
const left = hp + damage; // Oops! + instead of -
console.log("HP left: " + left);JavaScript has no way of knowing that `damage` was supposed to be subtracted instead of added. As far as JavaScript is concerned, `50 + 20` is a completely valid calculation!
The 3 types of errors
1. Syntax error β Broken grammar; the program cannot even start. 2. Runtime error β The program starts, then crashes on an impossible operation. 3. Logic error β The program runs without crashing, but calculates the wrong answer.
Exercise 8 β Three Kinds of Errors
Match each kind with its signature.
Exercise 9 β Diagnose the Scripts
Three broken battle scripts are described below. For each one, decide which kind of error it has and print your diagnosis. No code to fix here, only judgement.
Script A: prints `"start"`, then stops with a red message about `undefined`. Script B: prints absolutely nothing, and the message mentions an unexpected token. Script C: prints every line as planned, but the final score is twice what it should be. Step 1: `diagnose(letter, kind)` prints `"Script letter: kind"`. Step 2: call it once per script, using exactly `"syntax"`, `"runtime"` or `"logic"` as the kind.
Expected output:
Script A: runtime
Script B: syntax
Script C: logic// Write your function here
// Diagnose the three scripts here