JavaScript String valueOf()
Syntax & Examples
String.valueOf() method
The valueOf() method of the String class in JavaScript returns the primitive value of the specified string object.
Syntax of String.valueOf()
The syntax of String.valueOf() method is:
valueOf()
This valueOf() method of String returns the primitive value of the specified object. Overrides the Object.prototype.valueOf() method.
Return Type
String.valueOf() returns value of type String
.
✐ Example
1 Using valueOf() method
In JavaScript, the valueOf()
method returns the primitive value of a string object.
For example,
- We define a string object
strObj
using theString()
constructor. - We call the
valueOf()
method onstrObj
. - The primitive value of
strObj
is stored in the variableprimitiveValue
. - We log
primitiveValue
to the console using theconsole.log()
method.
JavaScript Program
const strObj = new String('Hello');
const primitiveValue = strObj.valueOf();
console.log(primitiveValue);
Output
Hello
Summary
In this JavaScript tutorial, we learned about valueOf() method of String: the syntax and few working examples with output and detailed explanation for each example.