.valueOf()

dorian-edwards's avatar
Published Oct 11, 2022
Contribute to Docs

The .valueOf() method returns the primitive value of a String object, similar to the .toString() method. Usually, this method is called internally by JavaScript.

Note: Objects that are descendants of the Object class, including Strings, inherit the .valueOf() method.

Syntax

stringObject.valueOf();

The .valueOf() method takes no parameters.

Example

All primitive string values are wrapped in a String object (new String(value)). In the following example, this primitive value is returned by the .valueOf() method:

const name = new String('Bill');
console.log(name);
console.log(name.valueOf());

This will print the following:

[String: 'Bill']
Bill

Codebyte Example

Although the .valueOf() method is typically invoked internally by JavaScript, it can be used by calling it on a String object:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy