TypeError
BaselineWidely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
TheTypeError
object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.
ATypeError
may be thrown when:
- an operand or argument passed to a function is incompatible with the type expected by that operator or function; or
- when attempting to modify a value that cannot be changed; or
- when attempting to use a value in an inappropriate way.
TypeError
is aserializable object, so it can be cloned withstructuredClone()
or copied betweenWorkers usingpostMessage()
.
TypeError
is a subclass ofError
.
Constructor
TypeError()
Creates a new
TypeError
object.
Instance properties
Also inherits instance properties from its parentError
.
These properties are defined onTypeError.prototype
and shared by allTypeError
instances.
TypeError.prototype.constructor
The constructor function that created the instance object. For
TypeError
instances, the initial value is theTypeError
constructor.TypeError.prototype.name
Represents the name for the type of error. For
TypeError.prototype.name
, the initial value is"TypeError"
.
Instance methods
Inherits instance methods from its parentError
.
Examples
Catching a TypeError
try { null.f();} catch (e) { console.log(e instanceof TypeError); // true console.log(e.message); // "null has no properties" console.log(e.name); // "TypeError" console.log(e.stack); // Stack of the error}
Creating a TypeError
try { throw new TypeError("Hello");} catch (e) { console.log(e instanceof TypeError); // true console.log(e.message); // "Hello" console.log(e.name); // "TypeError" console.log(e.stack); // Stack of the error}
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-native-error-types-used-in-this-standard-typeerror |