[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Error/toString [Back]  [Original]

Error.prototype.toString() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Error.prototype.toString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

toString() Error .

In this article

js
toString();

Error .

Error Object.prototype.toString() . (Object String ):

js
Error.prototype.toString = function () {
  "use strict";

  var obj = Object(this);
  if (obj !== this) {
    throw new TypeError();
  }

  var name = this.name;
  name = name === undefined ? "Error" : String(name);

  var msg = this.message;
  msg = msg === undefined ? "" : String(msg);

  if (name === "") {
    return msg;
  }
  if (msg === "") {
    return name;
  }

  return name + ": " + msg;
};

toString()

js
var e1 = new Error("fatal error");
console.log(e1.toString()); // 'Error: fatal error'

var e2 = new Error("fatal error");
e2.name = undefined;
console.log(e2.toString()); // 'Error: fatal error'

var e3 = new Error("fatal error");
e3.name = "";
console.log(e3.toString()); // 'fatal error'

var e4 = new Error("fatal error");
e4.name = "";
e4.message = undefined;
console.log(e4.toString()); // ''

var e5 = new Error("fatal error");
e5.name = "hello";
e5.message = undefined;
console.log(e5.toString()); // 'hello'

Specification
ECMAScript 2027 LanguageSpecification
# sec-error.prototype.tostring


Web Proxy Viewer  |  New URL  |  Original Page