JavaScript String valueOf() Method

JavaScript String valueOf() method is an inbuilt method in JavaScript that is used to return the value of the given string. The valueOf() method in JavaScript returns the primitive value of a string. It does not modify the original string. This method can also be used to convert a string object into a string.

Syntax: 

string.valueOf()

Parameters: It does not accept any parameter.

Return Values: It returns a string that represents the value of the given string object.

Below is an example of the string.valueOf() Method. 

Example 1: Here is the basic example if a string.valueOf() method.

javascript




let a = new String("w3wiki");
console.log(a.valueOf());


Output: 

w3wiki

Example 2: Another example of the above-explained-method.

javascript




// Taking a string as input and printing it
// with the help of string.valueOf() method
let a = new String("w3wiki");
console.log(a.valueOf());


Output: 

w3wiki

Example 3: In this example, using the valueOf() method to obtain their primitive string values.

javascript




// Taking a string as input and printing it
// with the help of string.valueOf() method
let a = new String("Beginner");
let b = new String("for");
let c = new String("Beginner");
console.log(a.valueOf(), b.valueOf(), c.valueOf());


Output: 

w3wiki

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browser:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 3 and above
  • Safari 1 and above


Contact Us