JavaScript .valueOf()

dorian-edwards's avatar
Published Oct 11, 2022

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.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

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

All contributors

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours