Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

String.prototype.toString() — retrieving a string value

String.prototype.toString() returns the primitive string value of a string or String object. It is not a general converter: an incompatible this value causes a TypeError.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

stringValue.toString()
String.prototype.toString.call(value)

The catalog symbol String.toString refers here to String.prototype.toString(), called on a string value. Do not confuse it with the expression String.toString(), which calls a method on the String function itself.

The method takes no parameters. For a primitive string, it returns that same value; for an object created with new String(...), it retrieves the text from the internal [[StringData]] slot. The result is a primitive of type string, not an object. The built-in String.prototype.valueOf() method returns the same value.

The method is not generic: String.prototype.toString.call(42) throws TypeError, as do calls with null, undefined, or an ordinary object without [[StringData]]. Merely having a custom toString method is insufficient. The example demonstrates valid receivers and deliberately catches the error for a number.

Examples

const text = "hello";
const wrapped = new String(text);
const result = wrapped.toString();

console.log(text.toString() === text);
console.log(result, typeof result);
console.log(result === wrapped.valueOf());

try {
  String.prototype.toString.call(42);
} catch (error) {
  console.log(error instanceof TypeError);
}

Result

true
hello string
true
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources