SyntaxError
Invalid JavaScript or invalid JSON parsing syntax.
Errors are signals that something went wrong. Good JavaScript code treats them as part of the program flow, not as random crashes.
try {
JSON.parse("{bad json}");
} catch (error) {
console.error(error.message);
}
Errors & Debugging
JavaScript throws errors when code cannot continue normally. Syntax errors, reference errors, type errors and custom errors all give clues about what failed.
A good error strategy separates expected problems from programmer mistakes. Invalid user input, failed network calls and bad JSON need handled paths. Broken assumptions need fixes.
Do not hide errors silently. A swallowed error removes the only evidence you had.
Invalid JavaScript or invalid JSON parsing syntax.
A name is used that does not exist in scope.
A value is used in a way its type does not support.
An application-specific failure with a clearer message.
Examples
function safeParse(value) {
try {
return { ok: true, data: JSON.parse(value) };
} catch (error) {
return { ok: false, message: error.message };
}
}
try {
runImportantTask();
} catch (error) {
// Nothing happens here.
}
Code patterns
These examples focus on the debugging patterns you will reuse: safe parsing, throwing, custom error types, console inspection, breakpoints and async failure handling.
The message explains the immediate failure.
try {
JSON.parse("{bad json}");
} catch (error) {
console.log(error.message);
}
Handle errors from external input.
function parseJson(value) {
try { return JSON.parse(value); }
catch { return null; }
}
Do not hide bugs caused by wrong assumptions.
const value = null; value.trim(); // TypeError
Return a structured result.
function safeParse(value) {
try { return { ok: true, data: JSON.parse(value) }; }
catch (error) { return { ok: false, error }; }
}
Rules that matter
Debugging starts before something breaks. Clear error types, helpful messages and visible failure states make the code easier to repair.
TypeError, SyntaxError and ReferenceError point to different problems.
Log, rethrow or convert to a visible result.
Bad input and failed requests deserve clear paths.
Do not hide bugs with broad catch blocks.
A message should help the next developer or user.
Add information about what operation failed.
Production thinking
Error handling decides whether a failure becomes a recoverable state or a mystery.
User-facing failures should become clear text status, not silent broken controls.
Production applications should report unexpected errors and handle expected errors locally.
Client-side render errors can leave pages empty or incomplete, which can hurt crawlability and user trust.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
A JavaScript feature is not production-ready until you know how it fails and how you will inspect that failure.