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);
Call
What 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.
No parameters. The method reads the name and message of the object it is called on.
Return Value
Return Value
Description
"name: message"
Both name and message have text. A missing name counts as "Error", and a missing message counts as an empty string.
name only
The message is an empty string.
message only
The 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);
Error: Disk is full
TypeError: Age must be a number
[Error]
Caught: Error: Disk is full
How It Works
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".
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.
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());
HttpError 404: Page not found
Joined: HttpError 404: Page not found
Template: HttpError 404: Page not found
Original: HttpError: Page not found
How It Works
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.
Used everywhere.String(), + and the template literal all call toString(), so all three print the custom text.
The original still works.Error.prototype.toString.call(notFound) runs the built-in version, which knows nothing about status.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerAll versions · Legacy
Full support
OperaAll versions
Full support
Samsung InternetAll versions · Android
Full support
Android WebViewAll versions
Full support
Node.jsAll versions · Server runtime
Supported
DenoAll versions · JavaScript runtime
Supported
BunAll 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.