JavaScript String valueOf() Method

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

What You’ll Learn

String.prototype.valueOf() returns this string’s primitive text (same API as MDN String.prototype.valueOf()). Learn how it unwraps new String(...) objects, how it matches toString(), why it is often called by the engine, five examples, and try-it labs.

01

Kind

Instance method

02

Returns

string primitive

03

Parameters

None

04

vs toString

Same result

05

Wrong this

TypeError

06

Baseline

Widely available

Introduction

Almost every string you write is already a primitive ("like this". valueOf() returns that primitive text — especially useful when you meet a String object from new String(...).

For String values, the result is equivalent to toString(). MDN notes this method is usually called internally by JavaScript during coercion, not typed out by hand.

💡
Beginner tip

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

This page is part of JavaScript String Methods. Related topics include toString() and localeCompare().

Understanding the valueOf() Method

str.valueOf() returns the underlying string primitive. On a String object it unwraps the wrapped text; on a literal it returns the same characters.

  • Returns a string primitive.
  • Equivalent to String.prototype.toString() for String values.
  • Requires this to be a string primitive or String object.
  • Throws TypeError for other this values (no coercion).

📝 Syntax

General form of String.prototype.valueOf:

JavaScript
str.valueOf()

Parameters

None.

Return value

A string representing the primitive value of a given String object (or the string itself).

Exceptions

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

Common patterns

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

⚡ Quick Reference

GoalCode
Get the primitive textstr.valueOf()
Unwrap a String objectnew String("a").valueOf()
Same via toStringstr.toString()
Coerce any value to stringString(value) or \`\${value}\`
Check typestypeof wrapper vs typeof wrapper.valueOf()

🔍 At a Glance

Four facts to remember about String.valueOf().

Returns
string

Primitive text

Args
none

Empty call

toString
same

Identical result

Wrong this
TypeError

No coercion

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

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

Examples Gallery

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

📚 Getting Started

MDN demos: String objects vs the primitive text.

Example 1 — Basic valueOf()

MDN Try it demo: a String object vs its primitive value.

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

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

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

How It Works

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

Example 2 — Hello world

MDN Using valueOf() demo.

JavaScript
const x = new String("Hello world");
console.log(x.valueOf());
// 'Hello world'
Try It Yourself

How It Works

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

🔧 Details Beginners Need

typeof, twin with toString, and TypeError.

Example 3 — Primitive vs Object

Calling valueOf() 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.valueOf());      // "string"
console.log(typeof wrapped);                // "object"
console.log(typeof wrapped.valueOf());      // "string"
console.log(literal.valueOf() === wrapped.valueOf()); // true
Try It Yourself

How It Works

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

Example 4 — Same as toString()

MDN: the value is equivalent to String.prototype.toString().

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

console.log(s.valueOf());
console.log(s.toString());
console.log(s.valueOf() === s.toString());
// 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.valueOf.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.valueOf for actual strings.

🎯 Common Use Cases

  • Unwrap String objects — turn new String(...) into a primitive.
  • Explicit APIs — document that you need a string primitive.
  • Understand coercion — engines often call valueOf for you.
  • Not for general coercion — prefer String(value).
  • Rare for literals"hi".valueOf() is valid but usually unnecessary.

🧠 How valueOf() 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 toString() for String.

📝 Notes

  • Equivalent to toString() for String values.
  • Usually invoked by the language during coercion — not always written in app code.
  • Avoid new String() in modern code unless you have a special reason.
  • Wrappers and primitives are not equal with === until you unwrap.
  • Do not confuse with Number/Boolean valueOf — those return other primitives.

Browser & Runtime Support

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

Baseline · Widely available

String.valueOf()

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

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
valueOf() Excellent

Bottom line: Use String.valueOf() to get the primitive text from a string or String object. It matches toString() for String; use String(value) for general conversion.

Conclusion

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

Continue with toString(), padEnd(), or the String methods hub.

💡 Best Practices

✅ Do

  • Prefer string literals over new String()
  • Use valueOf() to unwrap String objects when needed
  • Remember it equals toString() for String
  • Use String(x) for converting non-strings
  • Know the engine may call it during coercion

❌ Don’t

  • Expect coercion of numbers via this method
  • Assume wrappers and primitives compare equal with ===
  • Call it on every literal “just in case”
  • Confuse it with Number.valueOf
  • Override String.prototype.valueOf in production code

Key Takeaways

Knowledge Unlocked

Five things to remember about String.valueOf()

Returns the string primitive — same as toString().

5
Core concepts
🔢 02

Parameters

none

Args
🔄 03

toString

identical

Twin
04

Wrong this

TypeError

Limit
05

Prefer

literals

Style

❓ Frequently Asked Questions

String.prototype.valueOf() returns the primitive string value — the text itself for a literal, or the string wrapped by a String object. It takes no arguments.
For String, they are equivalent — same result. MDN notes valueOf returns the same value as toString(). Either can unwrap a String object.
Usually no. JavaScript often calls valueOf internally during coercion. You may call it explicitly to document that you want the primitive string.
Calling String.prototype.valueOf with a non-string this (for example via call on a number) throws TypeError. It does not coerce other types to string.
Use String(value) (or template literals) when you need to convert any type to a string. valueOf only works on string primitives or String objects.
No. It returns a string primitive. String objects and primitives are not mutated by the call.
Did you know?

MDN says valueOf is usually called internally by JavaScript. When code does something like adding a String object to another value, the engine may reach for valueOf / toString to get a primitive — which is why you rarely write it by hand.

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