SyntaxError — Parse-Time Failures
debt(d1/e1/b1/t7)
Closest to 'caught instantly (compiler/syntax error)' (d1). ESLint and TypeScript (both listed in detection_hints.tools) catch SyntaxErrors at parse time, before runtime. The syntax violation is immediately visible.
Closest to 'one-line patch or single-call swap' (e1). The quick_fix explicitly states wrapping JSON.parse() in try/catch is the remedy, which is a single-line addition. Fixing malformed JSON strings (quotes) or removing eval() is also trivial.
Closest to 'minimal commitment' (b1). SyntaxError handling is a localized concern—it affects only the code parsing JSON or evaluating strings. It does not impose structural debt across the codebase or shape future architectural choices. The choice to wrap JSON.parse() is a local best practice, not a load-bearing abstraction.
Closest to 'serious trap' (t7). The misconception field states the canonical wrong belief: 'A SyntaxError in a function only prevents that function from running — it prevents the entire script from executing.' This directly contradicts developer intuition (that errors are function-scoped) and causes silent failures in production when malformed JSON from external APIs crashes the entire feature. This trap is serious because the obvious (wrong) assumption is enforced by how other runtime errors behave in JavaScript, making it a cognitive mismatch with similar concepts.
TL;DR
Explanation
SyntaxError is thrown at parse time before any code runs. The entire script block fails — not just the broken function. Common causes: missing closing brackets/braces, invalid JSON in JSON.parse() (single quotes, trailing commas), using reserved words as identifiers (class, let, const in non-strict modes), template literal syntax errors, regex syntax errors. JSON.parse() throws SyntaxError for invalid JSON — always wrap in try/catch. In modules, import/export syntax errors prevent the entire module graph from loading.
Common Misconception
Why It Matters
Common Mistakes
- Not wrapping JSON.parse() in try/catch for external data.
- Using single quotes in JSON strings — JSON requires double quotes.
- eval() throwing SyntaxError — another reason to avoid eval.
Code Examples
// JSON with single quotes — SyntaxError:
const data = JSON.parse("{'name': 'Paul'}"); // SyntaxError
// Trailing comma in JSON:
const config = JSON.parse('{"debug": true,}'); // SyntaxError
try {
const data = JSON.parse(response);
} catch (e) {
if (e instanceof SyntaxError) {
console.error('Invalid JSON:', e.message);
} else {
throw e; // Re-throw non-syntax errors
}
}