TypeError
A TypeError occurs when a value is used in a way that is not allowed for its type.
Usually happens because:
- ☑ You accessed undefined
- ☑ You accessed null
- ☑ You called something that isn't a function
🔍 Quick Checklist:
What is TypeError?
A TypeError is a native JavaScript runtime error thrown when an operation is performed on a value that is incompatible with the expected type. Common scenarios include attempting to invoke a non-callable object (like calling a number as a function), querying properties on null or undefined values, or violating language constraints such as reassigning a read-only constant variable. Unlike compile-time errors, a TypeError is triggered dynamically when the JavaScript engine attempts to execute the invalid operation at runtime, causing execution to halt unless handled within a try-catch block.
Common Causes
- Accessing properties of undefined: Attempting to read a field of a variable that holds the value undefined.
- Accessing properties of null: Trying to look up keys or call functions on a null object.
- Calling a non-function: Executing parenthesis () on variables containing strings, numbers, arrays, or objects.
- Incorrect object type: Executing native methods on objects that do not define them.
- Invalid API response: Assuming an API returns defined JSON structures when it actually returns empty or null payloads.
- Incorrect DOM element lookup: Calling methods (like addEventListener) on a selector query that returned null.
- Writing to const: Reassigning values to variables declared with the const keyword.
- Invalid method usage: Using values in native method calls that do not match the expected API signature.
Common Mistakes
- Assuming external JSON payloads always contain nested child fields.
- Executing document.querySelector queries without verifying if elements exist in the DOM environment.
- Reusing constant bindings in loops where variables must be updated (use let instead).
How to Fix
Framework-Specific Examples
Attempting to invoke a non-function variable.
const age = 25;
try {
age();
} catch (err) {
console.error(err.message); // age is not a function
}Server Configuration Examples
Passing wrong parameter arguments to Node.js core filesystem modules.
const fs = require("fs");
try {
fs.readFileSync(); // Throws TypeError: The "path" argument must be of type string.
} catch (err) {
console.error(err.message);
}Best Practices
- Always validate external data inputs.
- Prefer optional chaining (?.) when querying deep object properties.
- Check for null and undefined definitions explicitly.
- Use TypeScript or JSDoc annotations to catch typing issues at compile time.
Frequently Asked Questions (FAQ)
Q: Is TypeError a syntax error?
No. A TypeError occurs while the code is running, whereas a SyntaxError prevents the code from running at all.
Q: Is TypeError only for browsers?
No. It can occur in browsers, Node.js, Deno, Bun, and other JavaScript runtimes.
Q: Can TypeScript prevent TypeError?
TypeScript can catch many type-related mistakes during development, but it cannot prevent every runtime TypeError.
Q: Is TypeError always caused by undefined?
No. It can also be caused by invalid function calls, null values, incorrect arguments, or incompatible object types.