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).
Kinds of error
| Error | What happens | Example |
|---|---|---|
| Syntax error | The code breaks the rules of the language, so the script does not run at all; the console reports a SyntaxError and a line number | A missing bracket or quote |
| Runtime error | The code starts but stops at a statement that cannot be done; the console reports, for example, a ReferenceError or TypeError | Calling consol.log, or using an element that is null |
| Logic error | The code runs without any message but gives the wrong result | Using > instead of >=, or + joining strings instead of adding numbers |
Finding errors
- Open the browser's developer tools (F12) and read the console: it gives the error type, message and line number. Fix the first error first.
- Use console.log() to display variable values at key points, to see where the values go wrong (a trace).
- Use the debugger: set a breakpoint on a line in the Sources tab, then step through line by line while watching the variables.
- Use the Elements tab to inspect the live DOM and the CSS applied to an element.
- Common causes: misspelt names (JavaScript is case-sensitive: getElementByID fails), a wrong id, a script running before the elements exist, missing brackets, = instead of === in a condition, and strings not converted to numbers.
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 finishedCode 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?
- The code runs without an error message but the result is wrong: a logic error.
- console.log(typeof a, typeof b) would show string string.
- 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().
Check yourself
- 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 >=).
- 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.)
- 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.
- 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;
- 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.
- 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).
- 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.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Information Technology specification (first teaching 2018), Unit 2 Topics 7 to 11
- Pearson qualifications: Edexcel International Advanced Level Information Technology (2018)
Last updated 8 November 2026. Spotted a mistake? Tell us.