CKTIS / JavaScript
TypeError — an invalid-operation error
TypeError is a built-in JavaScript error type, often indicating an operation incompatible with a value's type. You can catch it or explicitly throw it when validating function arguments.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
new TypeError(message)
TypeError(message)
throw new TypeError(message)TypeError is a built-in constructor for errors inheriting from Error, not a Web API interface. It commonly signals an operation that a value does not support, such as calling a non-function. The specification also uses it when no other built-in error type appropriately describes the failure.
On success, both new TypeError(message) and a call to TypeError(message) without new return a new error object rather than throwing it. Use throw to throw the resulting object explicitly. The message argument is optional, but converting it to a string can throw before an error object is returned. For example, new TypeError(Symbol()) throws a TypeError without an explicit throw statement.
Your functions can throw TypeError when an argument violates a type requirement. For a value of the correct type but outside the permitted range, RangeError is usually more appropriate. In the example, catch handles the expected error and rethrows other errors.
Do not base logic on exact engine-generated messages: their wording is not standardized. The example supplies its own message, making the expected output stable. An instanceof TypeError check is appropriate within the same realm, but can fail for errors from another realm, such as a separate iframe.
Examples
function shout(value) {
if (typeof value !== "string") {
throw new TypeError("Expected a string");
}
return value.toUpperCase();
}
try {
shout(42);
} catch (error) {
if (!(error instanceof TypeError)) {
throw error;
}
console.log(error.name + ": " + error.message);
}Result
TypeError: Expected a string
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.