JavaScript Error toString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Instance method

Definition

error.toString() returns one short line of text that describes an error: its name, a colon and a space, then its message. For example, new TypeError("Bad type").toString() returns "TypeError: Bad type".

JavaScript calls it for you whenever an error has to become text, for example in "Caught: " + err, String(err) or a template literal. The text has no stack trace, which makes it a good one-line summary for messages and logs.

Remember
name + ": " + message   → both have text
name                    → message is empty
message                 → name is empty
"Error"                 → name and message not set (defaults)

toString() reads name and message every time you call it.

Syntax

Call toString() on an error object. It takes no arguments. You can also let JavaScript call it for you with String() or +.

JavaScript
error.toString();
String(error);
CallWhat it does
err.toString()Returns text such as "TypeError: Bad type".
String(err)Same result. JavaScript calls toString() for you. "" + err and template literals do the same.
Error.prototype.toString.call(obj)Works on any object that has name and message, even when it is not an error.

Built into JavaScript (ES3); no import required. Siblings: Error.captureStackTrace() and Error.isError().

Library declarations
JavaScript
// Error.prototype.toString (ES3)
error.toString()

// returns string

Parameters

ParameterDescription
(none)No parameters. The method reads the name and message of the object it is called on.

Return Value

Return ValueDescription
"name: message"Both name and message have text. A missing name counts as "Error", and a missing message counts as an empty string.
name onlyThe message is an empty string.
message onlyThe name is an empty string.
TypeError (thrown)The method was called on something that is not an object, for example Error.prototype.toString.call("text").

JavaScript Examples

Example 1 — Turn Errors into Text

Call toString() on different errors, and see how + does it automatically.

JavaScript
const plain = new Error("Disk is full");
const typed = new TypeError("Age must be a number");
const empty = new Error();

console.log(plain.toString());
console.log(typed.toString());
console.log("[" + empty.toString() + "]");
console.log("Caught: " + plain);
Try It Yourself

How It Works

  1. The name comes from the type. An error made with new Error() has the name "Error", and one made with new TypeError() has the name "TypeError".
  2. No message, no colon. new Error() has an empty message, so toString() returns only the name. The square brackets in the code show where the text starts and ends.
  3. Automatic conversion. Joining "Caught: " with plain makes JavaScript call toString() for you.

Example 2 — Change the Name and the Message

One error object, four different results. toString() reads name and message each time it runs.

JavaScript
const err = new Error("Age must be positive");
console.log(err.toString());

err.name = "ValidationError";
console.log(err.toString());

err.name = "";
console.log(err.toString());

err.name = "ValidationError";
err.message = "";
console.log(err.toString());
Try It Yourself

How It Works

  1. Default name. Before any change the name is "Error", so the text is name: message.
  2. Your own name. Setting err.name = "ValidationError" changes the text at once, because nothing is stored in advance.
  3. Empty parts are skipped. An empty name leaves only the message, and an empty message leaves only the name. No colon appears when one side is empty.

Example 3 — Write Your Own toString()

A custom error class can replace toString() to add extra details such as a status code.

JavaScript
class HttpError extends Error {
  constructor(status, message) {
    super(message);
    this.name = "HttpError";
    this.status = status;
  }

  toString() {
    return this.name + " " + this.status + ": " + this.message;
  }
}

const notFound = new HttpError(404, "Page not found");

console.log(String(notFound));
console.log("Joined: " + notFound);
console.log(`Template: ${notFound}`);
console.log("Original: " + Error.prototype.toString.call(notFound));
Try It Yourself

How It Works

  1. Replace the method. The class defines its own toString(), which adds the status to the text. It is found before the one in Error.prototype.
  2. Used everywhere. String(), + and the template literal all call toString(), so all three print the custom text.
  3. The original still works. Error.prototype.toString.call(notFound) runs the built-in version, which knows nothing about status.

Browser & Runtime Support

Error.prototype.toString() has been part of JavaScript for a very long time (since ES3, 1999), so every browser and runtime supports it.

✓ Baseline · Widely available

Error.prototype.toString()

Supported in every browser, including Internet Explorer, and in Node.js, Deno and Bun.

100% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer All versions · Legacy
Full support
Opera All versions
Full support
Samsung Internet All versions · Android
Full support
Android WebView All versions
Full support
Node.js All versions · Server runtime
Supported
Deno All versions · JavaScript runtime
Supported
Bun All versions · JavaScript runtime
Supported
Error.prototype.toString() Excellent

Bottom line: Safe everywhere. The text name: message is defined by the language, so it looks the same in every engine, unlike the stack, which differs.

Frequently Asked Questions

A one-line string made from the name and the message of the error, joined with a colon and a space, for example "TypeError: Bad type". If one of them is empty, you get only the other one.
No. It returns only name and message. To see the list of functions that were running, read err.stack, which is a separate property, or log the whole error object with console.log(err).
The name starts as "Error" for every error, even when you extend the class. Set this.name = "MyError" in the constructor, and toString() will use it. Without that line the text begins with "Error".
When an error is turned into text: String(err), "text" + err, template literals like ${err}, and alert(err). console.log(err) is different: it prints the stack and extra details instead of the short text.
Yes. Error.prototype.toString.call({ name: "HttpError", message: "Not Found" }) returns "HttpError: Not Found". The method only needs an object with name and message; a string or a number as the target throws a TypeError.

Back to the Error methods

You have finished the three Error method guides. Open the index to see all of them side by side.

Error Methods index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful