JavaScript String toString() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Instance method

What You’ll Learn

String.prototype.toString() returns this string’s primitive text (same API as MDN String.prototype.toString()). Learn how it unwraps new String(...) objects, how it matches valueOf(), why wrong this throws TypeError, five examples, and try-it labs.

01

Kind

Instance method

02

Returns

string primitive

03

Parameters

None

04

vs valueOf

Same result

05

Wrong this

TypeError

06

Baseline

Widely available

Introduction

Almost every string you write is already a primitive ("like this". toString() still matters when you meet a String object from new String(...), or when you want an explicit “give me the text” call.

For String values, this method overrides Object.prototype.toString. It returns the string itself (primitive) or the text wrapped by the object — the same behavior as valueOf().

💡
Beginner tip

Prefer string literals "hi" over new String("hi"). When you already have a primitive, toString() just returns that same text.

This page is part of JavaScript String Methods. Related topics include toLowerCase() and localeCompare(). For numbers with a base (binary/hex), see Number toString().

Understanding the toString() Method

str.toString() returns the underlying string text. It does not take a radix — that feature belongs to Number.prototype.toString.

  • Returns a string primitive.
  • Same implementation as String.prototype.valueOf().
  • Requires this to be a string primitive or String object.
  • Throws TypeError for other this values (no coercion).

📝 Syntax

General form of String.prototype.toString:

JavaScript
str.toString()

Parameters

None.

Return value

A string representing the specified string value (the primitive text).

Exceptions

TypeError if this is not a string primitive or a String object.

Common patterns

JavaScript
"hello".toString();                 // "hello"
new String("foo").toString();       // "foo"
new String("Hi").valueOf();         // "Hi" (same text)
typeof new String("x");             // "object"
typeof new String("x").toString();  // "string"

⚡ Quick Reference

GoalCode
Get the string textstr.toString()
Unwrap a String objectnew String("a").toString()
Same via valueOfstr.valueOf()
Coerce any value to stringString(value) or \`\${value}\`
Number with a basenum.toString(16) (Number API)

🔍 At a Glance

Four facts to remember about String.toString().

Returns
string

Primitive text

Args
none

No radix

valueOf
same

Identical result

Wrong this
TypeError

No coercion

📋 toString() vs valueOf() vs String()

str.toString()str.valueOf()String(x)
Works onString onlyString onlyAny value
Result for "hi""hi""hi""hi"
Result for 42TypeError if forcedTypeError if forced"42"
Best forExplicit string textPrimitive unwrapGeneral conversion

Examples Gallery

Examples follow MDN String.toString() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN demos: String objects vs the primitive text.

Example 1 — Basic toString()

MDN Try it demo: log a String object, then its primitive string.

JavaScript
const stringObj = new String("foo");

console.log(typeof stringObj);
// "object"

console.log(stringObj.toString());
// "foo"
Try It Yourself

How It Works

new String("foo") creates a wrapper object. toString() returns the wrapped text as a normal string primitive.

Example 2 — Hello world

MDN Using toString() demo.

JavaScript
const x = new String("Hello world");

console.log(x.toString());
// "Hello world"
Try It Yourself

How It Works

Whatever text the String object wraps is what toString() returns — spaces and all.

🔧 Details Beginners Need

typeof, valueOf twin, and TypeError.

Example 3 — Primitive vs Object

Calling toString() on a literal still works — and stays a string.

JavaScript
const literal = "hello";
const wrapped = new String("hello");

console.log(typeof literal);                 // "string"
console.log(typeof literal.toString());      // "string"
console.log(typeof wrapped);                 // "object"
console.log(typeof wrapped.toString());      // "string"
console.log(literal.toString() === wrapped.toString()); // true
Try It Yourself

How It Works

Literals are already primitives. Wrappers are objects until you call toString() (or valueOf()).

Example 4 — Same as valueOf()

MDN notes both methods share the same implementation for String.

JavaScript
const s = new String("CodeToFun");

console.log(s.toString());
console.log(s.valueOf());
console.log(s.toString() === s.valueOf());
// true
Try It Yourself

How It Works

Pick whichever name fits your code. For String, they return the same text.

Example 5 — Wrong this Throws

Unlike String(value), this method refuses non-string receivers.

JavaScript
try {
  String.prototype.toString.call(42);
} catch (err) {
  console.log(err.name);
  // TypeError
}

console.log(String(42));
// "42" — general conversion still works
Try It Yourself

How It Works

Use String(x), template literals, or concatenation when you need to convert any value. Reserve String.prototype.toString for actual strings.

🎯 Common Use Cases

  • Unwrap String objects — turn new String(...) into a primitive.
  • Explicit APIs — document that you need a string primitive.
  • Not for numbers — use Number.prototype.toString(radix) or String(n).
  • Not for general coercion — prefer String(value).
  • Rare for literals"hi".toString() is valid but usually unnecessary.

🧠 How toString() Works

1

Check this

Must be a string primitive or String object.

Guard
2

Reject other types

Non-string this → TypeError (no ToString coerce).

Error
3

Read the text

Take the primitive or the wrapped string contents.

Extract
4

Return a string primitive

Same result as valueOf() for String.

📝 Notes

  • Overrides Object.prototype.toString — you do not get "[object String]" from this method.
  • Identical to valueOf() for String values.
  • String objects used in template literals call toString(); string primitives are already strings, so they skip that path.
  • Avoid new String() in modern code unless you have a special reason.
  • Do not confuse with Number.toString(radix).

Browser & Runtime Support

String.prototype.toString() is Baseline Widely available — part of the language from early editions and safe everywhere.

Baseline · Widely available

String.toString()

Safe for production in browsers and runtimes (Node.js, Deno, Bun). Simple unwrap of String objects to primitives.

Universal Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · 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
toString() Excellent

Bottom line: Use String.toString() to get the primitive text from a string or String object. Use String(value) for general conversion, and Number.toString for numeric bases.

Conclusion

String.prototype.toString() returns the underlying string text — especially useful when unwrapping new String(...). It matches valueOf(), takes no radix, and throws if this is not a string.

Continue with toLowerCase(), replace(), or the String methods hub.

💡 Best Practices

✅ Do

  • Prefer string literals over new String()
  • Use toString() to unwrap String objects when needed
  • Use String(x) for converting non-strings
  • Remember it equals valueOf() for String
  • Link learners to Number.toString for radix needs

❌ Don’t

  • Pass a radix to String.toString (not supported)
  • Expect coercion of numbers via this method
  • Confuse it with Object.prototype.toString
  • Override String.prototype.toString in production code
  • Assume wrappers and primitives compare equal with ===

Key Takeaways

Knowledge Unlocked

Five things to remember about String.toString()

Returns the string primitive — same as valueOf().

5
Core concepts
🔢 02

Parameters

none

Args
🔄 03

valueOf

identical

Twin
04

Wrong this

TypeError

Limit
05

Prefer

literals

Style

❓ Frequently Asked Questions

String.prototype.toString() returns the string primitive itself (for a string literal) or the string wrapped by a String object. It takes no arguments.
For String, they have the same implementation. Both return the underlying string text. Prefer either for unwrapping a String object; beginners often meet toString() first.
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 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. String objects and primitives are not mutated by the call.
Did you know?

String has no Symbol.toPrimitive. When a String object is used where a string is expected (like a template literal), JavaScript calls toString(). A string primitive is already a string, so that call is skipped.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

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.

8 people found this page helpful