{
    "slug": "js_type_errors",
    "term": "TypeError — Common Causes and Fixes",
    "category": "javascript",
    "difficulty": "beginner",
    "short": "TypeError is thrown when a value is not of the expected type — most commonly 'Cannot read properties of undefined/null', the most frequent JavaScript runtime error.",
    "long": "TypeError occurs when an operation is performed on an incompatible type. The most common: accessing a property on undefined or null. This happens when API responses have unexpected shapes, optional chaining is missing, or async data has not loaded yet. Other common TypeErrors: calling something that is not a function, accessing .length on null, passing wrong argument types. TypeScript catches most TypeErrors at compile time. In PHP, similar errors manifest as 'Call to a member function on null'.",
    "aliases": [
        "Cannot read properties of undefined",
        "is not a function",
        "null is not an object"
    ],
    "tags": [
        "javascript",
        "errors",
        "debugging",
        "runtime"
    ],
    "misconception": "TypeErrors only happen with bad code — they frequently happen with correct code that receives unexpected API responses or race conditions in async loading.",
    "why_it_matters": "TypeError from null/undefined is the most common JavaScript runtime error in production — optional chaining (?.) and nullish coalescing (??) prevent the majority of them.",
    "common_mistakes": [
        "Not using optional chaining for potentially-null API response properties",
        "Forgetting async data is not available synchronously",
        "Not checking Array.isArray() before calling array methods on unknown values"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "js_optional_chaining",
        "js_nullish_coalescing",
        "js_async_await"
    ],
    "prerequisites": [
        "js_optional_chaining",
        "js_async_await"
    ],
    "refs": [
        "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_read_property"
    ],
    "bad_code": "const user = null;\nconsole.log(user.name); // TypeError: Cannot read properties of null\n\nconst num = 42;\nnum.toUpperCase(); // TypeError: num.toUpperCase is not a function",
    "good_code": "// Optional chaining:\nconsole.log(user?.name); // undefined, no throw\n\n// Type check before calling:\nif (typeof value === 'string') {\n    value.toUpperCase();\n}\n\n// TypeScript catches these at compile time",
    "quick_fix": "Enable TypeScript strict mode or use optional chaining (?.) on all property accesses from external data — catches the majority of TypeError sources at development time",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/js_type_errors",
        "html_url": "https://codeclaritylab.com/glossary/js_type_errors",
        "json_url": "https://codeclaritylab.com/glossary/js_type_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": "[TypeError — Common Causes and Fixes](https://codeclaritylab.com/glossary/js_type_errors) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/js_type_errors"
            }
        }
    }
}