All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Error handling and debugging JavaScript

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Code rarely works first time. You need to recognise the kinds of error, use the browser's console and developer tools to find them, and handle errors with try and catch (spec 9.3.5).

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Kinds of error

ErrorWhat happensExample
Syntax errorThe code breaks the rules of the language, so the script does not run at all; the console reports a SyntaxError and a line numberA missing bracket or quote
Runtime errorThe code starts but stops at a statement that cannot be done; the console reports, for example, a ReferenceError or TypeErrorCalling consol.log, or using an element that is null
Logic errorThe code runs without any message but gives the wrong resultUsing > instead of >=, or + joining strings instead of adding numbers

Finding errors

try, catch and finally

function parseAge(text) {
  const age = Number(text);
  if (isNaN(age)) {
    throw new Error("Age must be a number");
  }
  return age;
}
try {
  console.log(parseAge("17"));
  console.log(parseAge("seventeen"));
  console.log("This line is skipped");
} catch (err) {
  console.log("Problem: " + err.message);
} finally {
  console.log("Check finished");
}

Output:

17
Problem: Age must be a number
Check finished

Code in try runs until an error is thrown; control jumps to catch, which receives the error object (err.message is its text); finally runs either way. throw creates your own error. This lets a page show a helpful message instead of failing silently.

Worked example

A total shows as 1020 instead of 30 when the boxes hold 10 and 20, and no error appears in the console. What kind of error is it and how would you find it?

  1. The code runs without an error message but the result is wrong: a logic error.
  2. console.log(typeof a, typeof b) would show string string.
  3. The values were joined, not added; convert them with Number().

Answer: A logic error caused by adding strings; trace with console.log and fix with Number().

Key idea: Syntax errors stop the script running; runtime errors stop it part way with an error such as ReferenceError or TypeError; logic errors give wrong results silently. Use the console, console.log traces, breakpoints and the Elements tab. try runs code, catch handles errors, finally always runs.

Check yourself

  1. A program runs with no error messages, but a mark of 40 is graded Fail when it should be Pass. What type of error is this?
    • Syntax error
    • Logic error
    • Runtime error
    • Compilation error
    Show answer

    Logic error. The code runs but produces the wrong result: a logic error (for example > used instead of >=).

  2. Which error does `consol.log("Hi");` cause?
    • SyntaxError
    • TypeError
    • RangeError
    • ReferenceError
    Show answer

    ReferenceError. consol is not defined, so a ReferenceError is thrown when the line runs. (Node and browsers report: consol is not defined.)

  3. What is displayed when this code runs? (Output lines are separated by / in the options.) ```js try { console.log("A"); undefinedFunction(); console.log("B"); } catch (e) { console.log("C"); } console.log("D"); ```
    • A / B / C / D
    • A / C / D
    • A / B / D
    • A / C
    Show answer

    A / C / D. A is displayed, the call throws a ReferenceError so B is skipped and catch displays C, then the program continues with D: A, then C, then D.

  4. This code should display Total 6 but it does not. Which line number contains the error? ``` 1 const out = document.getElementById("out"); 2 let total = 0; 3 for (let i = 1; i <= 3; i++) { 4 total = total + i; 5 } 6 out.textcontent = "Total " + total; 7 console.log(out.textContent); ```
    Show answer

    6. JavaScript is case-sensitive: textcontent creates a new, unused property, so the paragraph never changes. The fix is out.textContent = "Total " + total;

  5. Which browser tool shows JavaScript error messages with line numbers?
    • The console in the developer tools
    • The address bar
    • The bookmarks menu
    • The print preview
    Show answer

    The console in the developer tools. Open the developer tools (F12) and look at the console.

  6. When does the code in a finally block run?
    • Only when an error is thrown
    • Only when no error is thrown
    • Whether or not an error was thrown in the try block
    • Only when the page is closed
    Show answer

    Whether or not an error was thrown in the try block. finally always runs after try (and catch).

  7. Explain how console.log() helps to find a logic error.
    Show answer

    By displaying the values of variables at different points in the code, you can see where a value first becomes wrong.. Logging variable values at key points traces the program and shows where it goes wrong.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.