SyntaxError — Parse-Time Failures
TL;DR
SyntaxError means JavaScript couldn't parse your code — the entire script fails to execute. Common causes: missing brackets, invalid JSON, reserved word misuse.
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
✗ A SyntaxError in a function only prevents that function from running — it prevents the entire script from executing.
Why It Matters
SyntaxErrors in production scripts cause complete feature failure. JSON.parse() SyntaxErrors from malformed API responses are a common source of uncaught errors.
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
✗ Vulnerable
// JSON with single quotes — SyntaxError:
const data = JSON.parse("{'name': 'Paul'}"); // SyntaxError
// Trailing comma in JSON:
const config = JSON.parse('{"debug": true,}'); // SyntaxError
✓ Fixed
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
}
}
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
22 Mar 2026
Edited
5 Apr 2026
Views
24
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 5
Unknown AI 4
ChatGPT 3
Google 2
Perplexity 2
Majestic 1
Ahrefs 1
Also referenced
How they use it
crawler 15
crawler_json 1
pre-tracking 2
Related categories
⚡
DEV INTEL
Tools & Severity
🟠 High
⚙ Fix effort: Low
⚡ Quick Fix
Always wrap JSON.parse() in try/catch. Use JSON double quotes. Validate JSON with a linter. Use TypeScript to catch syntax errors at compile time.
📦 Applies To
javascript ES5
web
cli
🔗 Prerequisites
🔍 Detection Hints
JSON.parse\(
Auto-detectable:
✓ Yes
eslint
typescript
⚠ Related Problems
🤖 AI Agent
Confidence: High
False Positives: Low
✗ Manual fix
Fix: Low
Context: Function
Tests: Update