.valueOf()
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, includingString
s, 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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours