World’s largest virtual agentic engineering & quality conference
TypeError is thrown when you perform an operation on a value whose type does not support it. This guide covers the common TypeErrors developers hit, how to fix each one, and how to prevent them.
Saif Sadiq
Author
Last Updated on: July 16, 2026
Can you add a number and an alphabet?
Say, if I ask you to give me the result of the addition of 1 and H will you be able to give me the answer?
The obvious answer is NO.
Same goes in JavaScript. When you try to perform an operation on two operands of unmatched type, JavaScript throws a TypeError. In technical terms, a TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function.
TypeError is one of the most frequent runtime errors in JavaScript, alongside a handful of others covered in this guide to common JavaScript errors. This article walks through the specific TypeErrors developers hit most, how to fix each, and how to stop them before they reach production.
Overview
To resolve and prevent JavaScript TypeErrors, use TypeScript for compile-time type safety and run scripts on TestMu AI to catch engine-specific runtime failures. Developers can also fix these errors during runtime by implementing typeof checks, optional chaining, and nullish coalescing to handle unexpected values safely.
For example, you will get an Uncaught TypeError if you try to convert a number to uppercase. toUpperCase() is a string method, so calling it on a number is an incompatible operation. The following code throws an error.
Code structure
var num = 1;
try {
num.toUpperCase();
}
catch (err) {
document.getElementById("demo").innerHTML = err.name;
}
Error

The fastest guard is to check the type before calling the method. Use the typeof operator so the string method only runs on an actual string.
var num = 1;
if (typeof num === "string") {
num.toUpperCase(); // only runs when num is really a string
} else {
console.log("Not a string, skipping toUpperCase()");
}
If you actually need the uppercase output, convert the number to a string first. There are two common ways to do it.
Use toString() to convert the number into a string first, then call toUpperCase() on the result.
var num = 1;
try {
num.toString().toUpperCase(); // convert the number into a string first
}
catch (err) {
document.getElementById("demo").innerHTML = err.name;
}
Output
"1"
The String() constructor of the predefined String class wraps the number as a string object, so string methods become available.
var num = 1;
num = new String(num);
try {
num.toUpperCase(); // now valid, num is a String object
}
catch (err) {
document.getElementById("demo").innerHTML = err.name;
}
Some TypeErrors, like the browser-specific messages older code threw for console.log or prompt, only appear in certain engines. Running your scripts across a real browser matrix surfaces those environment-specific failures before your users do.
Beyond the toUpperCase example, three TypeError messages account for most real-world crashes. Here is what each means and the modern way to fix it.
This is the single most common TypeError. It fires when you access a property or method on a value that is undefined or null, usually because an object or array field you expected was not there.
Before
const user = { name: "Ada" };
// user.profile was never set
console.log(user.profile.city);
// Uncaught TypeError: Cannot read properties of undefined (reading 'city')
After - use optional chaining (?.) so the access short-circuits to undefined instead of throwing, and nullish coalescing (??) to supply a fallback value.
const user = { name: "Ada" };
// ?. stops the chain the moment profile is undefined
const city = user?.profile?.city ?? "Unknown";
console.log(city); // "Unknown"
Note that ?. is what prevents the error by guarding the read; ?? only decides what value to use when the result is null or undefined.
This TypeError appears when you call something that is not a function, often a misspelled method, a property that holds a value instead of a function, or a variable reassigned to a non-function.
Before
const list = [1, 2, 3];
// pushh is a typo, it is not a function
list.pushh(4);
// Uncaught TypeError: list.pushh is not a function
After - call the correct method, and when a value might not be callable, verify it with typeof first.
const list = [1, 2, 3];
list.push(4); // correct method name
// guard a value that may or may not be a function
if (typeof callback === "function") {
callback();
}
Reassigning a variable declared with const throws a TypeError. A const binding cannot be reassigned after it is created.
Before
const total = 10;
total = 20;
// Uncaught TypeError: Assignment to constant variable.
After - use let when the value needs to change, and keep const for values that never do.
let total = 10;
total = 20; // valid, let allows reassignment
The two errors are easy to confuse because both often involve undefined, but they point to different root causes. A ReferenceError means the variable was never declared, so the name does not exist in scope. A TypeError means the variable exists but the operation you attempted is not valid for its current type.
// ReferenceError: the name was never declared
console.log(score);
// Uncaught ReferenceError: score is not defined
// TypeError: the variable exists but the operation is invalid for its type
let score = 5;
score.toUpperCase();
// Uncaught TypeError: score.toUpperCase is not a function
| Aspect | ReferenceError | TypeError |
|---|---|---|
| Cause | Accessing a variable that was never declared | Performing an operation incompatible with a value's type |
| Does the name exist? | No, the identifier is not in scope | Yes, the variable exists but holds the wrong type |
| Typical message | x is not defined | x is not a function, Cannot read properties of undefined |
| Fix | Declare the variable or correct the spelling | Type-check with typeof or guard with optional chaining |
Fixing a TypeError after it crashes is reactive. These practices stop most of them from ever running.
typeof value === "function" and typeof value === "string" catch the two most common TypeError triggers.?. and provide safe defaults with ??, so a missing field returns a fallback instead of crashing.try...catch block lets you handle the failure gracefully rather than halting execution.Note: TypeErrors often surface only on specific browsers. Test your JavaScript across real browser and OS environments with TestMu AI. Try TestMu AI Today!
Author
Saif Sadiq is a community contributor with 7+ years of experience working across product, growth, and developer-focused platforms. Currently Director of Product & Growth at Apptile, he leads product strategy and cross-functional execution for no-code mobile app tooling. Saif previously worked at TestMu AI, contributing to product and growth initiatives for a cloud-based cross-browser testing platform, and has been recognized as a most-viewed blogger and writer.
Did you find this page helpful?
More Related Blogs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance