TypeError
JavaScript Programming Language
Severity: ModerateWhat Does This Error Mean?
A TypeError means JavaScript tried to do something with a value that is the wrong type. For example, calling a number like a function, or treating null like an object. This usually happens when a variable holds a different kind of value than your code expects.
Affected Models
- All browsers
- Node.js
- Deno
- Any JavaScript runtime
Common Causes
- Calling something as a function when it is not a function
- Trying to access a property on null or undefined
- Using a method that does not exist on that type of value
- Passing the wrong data type to a built-in JavaScript method
- A variable was never assigned a value before being used
How to Fix It
-
Read the full error message in your browser console or terminal. It will tell you the exact line number where the error happened.
Press F12 in your browser to open the Console tab. The error message is clickable and takes you to the exact line.
-
Check what the variable actually contains at that point. Add console.log(myVariable) just before the line that crashes.
You may find the variable is null, undefined, or a different type than you expected.
-
If the error says 'cannot read properties of null', add a check before using the variable: if (myVariable !== null) { ... }
This is called a null check. It prevents the code from running when the value is missing.
-
If the error says 'is not a function', make sure you are calling the right thing. Check for typos in the function name.
For example, writing arr.lenght() instead of arr.length will cause a TypeError.
-
Use optional chaining (?.) when accessing nested properties to avoid crashing on null values: myObject?.property?.subProperty
If any part of the chain is null or undefined, JavaScript returns undefined instead of throwing an error.
When to Call a Professional
TypeErrors are almost always fixable by the developer. If you are stuck and cannot find the problem, post the full error message and code snippet on Stack Overflow. Most TypeErrors have simple solutions once the root cause is identified.
Frequently Asked Questions
What is the difference between TypeError and ReferenceError?
A ReferenceError means the variable name does not exist at all — JavaScript has never heard of it. A TypeError means the variable exists, but you are trying to use it in a way that does not work for its type. Example: if myVar is not declared at all, you get ReferenceError. If myVar is null and you try to call myVar.toUpperCase(), you get TypeError.
Why do TypeErrors happen so often with null and undefined?
Because null and undefined have no properties or methods. When you write myVar.name and myVar is null, there is no 'name' to look up. JavaScript throws a TypeError to tell you the operation is impossible. Always check that a variable has a real value before accessing its properties.
Can a TypeError crash my whole website?
Yes, if it is not handled, a TypeError will stop the JavaScript on that page from running. This can break interactive features like buttons, forms, and menus. Wrap risky code in a try/catch block to handle errors gracefully and keep the rest of the page working.