JavaScript String valueOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.valueOf() returns the primitive string for a string literal or a String object. It takes no parameters and does not mutate the value.

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

Syntax

Call string.valueOf. Start with this line.

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

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

Library declarations
JavaScript
string.valueOf()

Parameters

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

Return Value

Return ValueDescription
*Primitive string

JavaScript Examples

Example 1 — Literal and String Object

Call valueOf() on a literal and on a wrapper object.

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

console.log(literal.valueOf());  // "Hello"
console.log(wrapped.valueOf());  // "Hello"
Try It Yourself

How It Works

  1. Literal case. "Hello" is already a primitive; valueOf() returns that text.
  2. Wrapper case. new String("Hello") is an object that stores the same text.
  3. Unwrap. wrapped.valueOf() returns the primitive "Hello".
  4. Same text. Both calls print Hello — the difference is object vs primitive before the call.

Example 2 — valueOf() vs toString()

For String, both methods return the same primitive text.

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

console.log(wrapped.valueOf());   // "CodeToFun"
console.log(wrapped.toString());  // "CodeToFun"
console.log(wrapped.valueOf() === wrapped.toString()); // true
Try It Yourself

How It Works

  1. Start with a wrapper. new String("CodeToFun") is type "object".
  2. valueOf unwraps. Returns the primitive "CodeToFun".
  3. toString matches. For String, toString() returns the same text.
  4. Strict equal. Both results are the same primitive → true.

Example 3 — typeof Before and After

See the type change when you unwrap a String object.

JavaScript
const wrapped = new String("Hi");

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

How It Works

  1. Wrapper type. typeof wrapped is "object", not "string".
  2. After valueOf. The result is a primitive → typeof is "string".
  3. Value match. That primitive equals "Hi" with ===.
  4. Rule of thumb. Prefer literals so you rarely need this unwrap step.

Browser & Runtime Support

String.prototype.valueOf() is a long-standing core method — available in all modern browsers and every supported Node.js release.

✓ Baseline · Widely available

String.prototype.valueOf()

Supported everywhere JavaScript runs. Safe for all targets; no polyfill needed for modern or legacy browsers that still matter.

100% Universal support
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() Universal

Bottom line: Use freely when you need the primitive string. Prefer literals day to day, and use String(value) when converting non-string types.

Frequently Asked Questions

valueOf() returns the primitive string value — the text itself for a literal, or the string wrapped by a String object. It takes no arguments and does not change the original value.
For String, they are equivalent — same result. Either can unwrap a String object to a primitive string.
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 a template literal) when you need to convert any type to a string. valueOf only works on string primitives or String objects.
Prefer string literals ("text") over new String("text"). Wrappers are rarely needed; valueOf mainly helps you understand and unwrap them.

Next: padEnd()

Learn how to pad a string to a target length from the end.

padEnd() 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.

7 people found this page helpful