{
    "slug": "js_syntax_errors",
    "term": "SyntaxError — Parse-Time Failures",
    "category": "javascript",
    "difficulty": "beginner",
    "short": "SyntaxError means JavaScript couldn't parse your code — the entire script fails to execute. Common causes: missing brackets, invalid JSON, reserved word misuse.",
    "long": "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.",
    "aliases": [],
    "tags": [
        "javascript",
        "errors",
        "json",
        "parsing"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "js_type_errors",
        "js_reference_errors",
        "js_json_parse_stringify"
    ],
    "prerequisites": [
        "js_json_parse_stringify"
    ],
    "refs": [
        "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token"
    ],
    "bad_code": "// JSON with single quotes — SyntaxError:\nconst data = JSON.parse(\"{'name': 'Paul'}\"); // SyntaxError\n\n// Trailing comma in JSON:\nconst config = JSON.parse('{\"debug\": true,}'); // SyntaxError",
    "good_code": "try {\n    const data = JSON.parse(response);\n} catch (e) {\n    if (e instanceof SyntaxError) {\n        console.error('Invalid JSON:', e.message);\n    } else {\n        throw e; // Re-throw non-syntax errors\n    }\n}",
    "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.",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-04-05",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/js_syntax_errors",
        "html_url": "https://codeclaritylab.com/glossary/js_syntax_errors",
        "json_url": "https://codeclaritylab.com/glossary/js_syntax_errors.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[SyntaxError — Parse-Time Failures](https://codeclaritylab.com/glossary/js_syntax_errors) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/js_syntax_errors"
            }
        }
    }
}