Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TypeError

BaselineWidely available *

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 newTypeError 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. ForTypeError instances, the initial value is theTypeError constructor.

TypeError.prototype.name

Represents the name for the type of error. ForTypeError.prototype.name, the initial value is"TypeError".

Instance methods

Inherits instance methods from its parentError.

Examples

Catching a TypeError

js
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

js
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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp