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,

  1. We define a string object strObj using the String() constructor.
  2. We call the valueOf() method on strObj.
  3. The primitive value of strObj is stored in the variable primitiveValue.
  4. We log primitiveValue to the console using the console.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.