JavaScript String toString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.toString() returns the string primitive for a string literal or a String object. It takes no parameters and never mutates the value.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.toString. Start with this line.

JavaScript
string.toString()
CallWhat it does
string.toString()Primary signature.
string.toString(...)Call with the arguments your case needs.

Built into JavaScript. Call toString(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
string.toString()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*String primitive

JavaScript Examples

Example 1 — Literal & String Object

See toString() on a primitive and on new String(...).

JavaScript
const literal = "Hello";
const obj = new String("Hello");

console.log(literal.toString());  // "Hello"
console.log(typeof literal);      // "string"

console.log(obj.toString());      // "Hello"
console.log(typeof obj);          // "object"
console.log(typeof obj.toString()); // "string"
Try It Yourself

How It Works

  1. Literal path. "Hello" is already a primitive; toString() returns the same text.
  2. Object path. new String("Hello") is an object wrapper (typeof → "object").
  3. Unwrap. obj.toString() yields the primitive "Hello".
  4. Why it helps. Use it when an API hands you a String object and you need a real string.

Example 2 — toString() vs valueOf()

For String, both methods return the same underlying text.

JavaScript
const obj = new String("Code");

console.log(obj.toString());              // "Code"
console.log(obj.valueOf());               // "Code"
console.log(obj.toString() === obj.valueOf()); // true
Try It Yourself

How It Works

  1. Build a wrapper. new String("Code") stores the text inside an object.
  2. Call toString. Returns the primitive "Code".
  3. Call valueOf. Also returns "Code" for String.
  4. Same result. Equality is true — pick either when unwrapping.

Example 3 — Prefer String() for Other Types

String.prototype.toString does not coerce numbers — use String(x).

JavaScript
console.log(String(42));           // "42"
console.log((42).toString());      // "42"  (Number.toString)
console.log(`${true}`);            // "true"

try {
  String.prototype.toString.call(42);
} catch (e) {
  console.log(e.name);             // "TypeError"
}
Try It Yourself

How It Works

  1. General coerce. String(42) safely turns many values into text.
  2. Number’s own method. (42).toString() is Number.prototype.toString, not String’s.
  3. Templates work too. `${true}` coerces with default rules.
  4. Wrong this throws. Forcing String’s toString onto a number → TypeError.

Browser & Runtime Support

String.prototype.toString() is Baseline Widely available — supported in every modern browser and legacy environments for decades.

✓ Baseline · Widely available

String.prototype.toString()

Supported in all evergreen browsers, Internet Explorer, and every current Node.js / Deno / Bun release.

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 IE 4+ · Legacy environments
Full support
Opera All modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
String.toString() Universal

Bottom line: Safe everywhere. Use it to unwrap String objects or document an explicit string result; use String(x) when you need to coerce non-string values.

Frequently Asked Questions

String.prototype.toString() returns the string primitive — for a literal that is the string itself; for a String object it unwraps the text inside. It takes no arguments.
For String they behave the same: both return the underlying string text. Beginners often meet toString() first; either is fine for unwrapping a String object.
Rarely for string primitives — they are already strings. It is useful when you have a String object from new String(...) and want a primitive, or when documenting an explicit conversion.
Calling String.prototype.toString with a non-string this (for example via call/apply on a number) throws TypeError. It does not coerce other types to string.
No. Number.toString() can take a radix (base 2–36). String.toString() has no radix — it only returns the string value.
No. It returns a string primitive. The original literal or String object is not mutated.

Next: valueOf()

Learn the twin method that also returns the underlying string primitive.

valueOf() tutorial →

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.

9 people found this page helpful