Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Error
  6. toString()

Error.prototype.toString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

ThetoString() method ofError instances returns a string representing this error.

Syntax

js
toString()

Parameters

None.

Return value

A string representing the specifiedError object.

Description

TheError object overrides theObject.prototype.toString()method inherited by all objects. Its semantics are as follows:

js
Error.prototype.toString = function () {  if (    this === null ||    (typeof this !== "object" && typeof this !== "function")  ) {    throw new TypeError();  }  let name = this.name;  name = name === undefined ? "Error" : `${name}`;  let msg = this.message;  msg = msg === undefined ? "" : `${msg}`;  if (name === "") {    return msg;  }  if (msg === "") {    return name;  }  return `${name}: ${msg}`;};

Examples

Using toString()

js
const e1 = new Error("fatal error");console.log(e1.toString()); // "Error: fatal error"const e2 = new Error("fatal error");e2.name = undefined;console.log(e2.toString()); // "Error: fatal error"const e3 = new Error("fatal error");e3.name = "";console.log(e3.toString()); // "fatal error"const e4 = new Error("fatal error");e4.name = "";e4.message = undefined;console.log(e4.toString()); // ""const e5 = new Error("fatal error");e5.name = "hello";e5.message = undefined;console.log(e5.toString()); // "hello"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-error.prototype.tostring

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp