.valueOf()
Anonymous contributor
Anonymous contributor88 total contributions
Anonymous contributor
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:
All contributors
- Anonymous contributorAnonymous contributor88 total contributions
- Anonymous contributor
Looking to contribute?
- 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.