This guide explains the usage of the String “valueOf()” method in JavaScript.
How to Use the String “valueOf()” Method in JavaScript?
The String “valueOf()” is an inbuilt JavaScript method invoked by JavaScript automatically to return the primitive value of a String. This method is also useful for converting a string object into a string.
Syntax
Here, “string” corresponds to the string whose value needs to be fetched.
Let’s use the above syntax to check the particular string value with the help of a practical example.
HTML Code
First, have a look at the following HTML code:
<button ondblclick="demo()">Double Click</button>
<p id="sample"></p>
In the above code:
- The “<h3>” tag creates a subheading.
- Next, create a button having an associated “ondblclick” event redirecting to the function named “demo()” that will be triggered upon the button double-click.
- After that, the “<p>” tag creates an empty paragraph with an id “sample” to display the primitive value i.e., string, numbers, boolean, symbol, and undefined types.
Note: The above HTML code will follow throughout all examples of this write-up.
Example 1: Applying the String “valueOf()” Method to Get a Particular String Value
In this example, the String “valueOf()” method is used to get the value of the defined string value.
JavaScript Code
Overview of the following JavaScript code:
function demo() {
var str = " Welcome to Linuxhint";
var result = str.valueOf();
document.getElementById("sample").innerHTML = result;
}
</script>
In the above lines of code:
- Define a function named “demo()”.
- In its definition, first declare the variable “str” to store the stated string.
- Next, the variable “result” utilizes the “valueOf()” method to get the value of the initialized string.
- Lastly, apply the “document.getElementById()” method to fetch the value of the paragraph via its id “sample” to display the primitive value of the string.
Output
The output displays the value of the initialized string upon the button double-click.
Example 2: Applying the String “valueOf()” Method to Get a Particular String Value(Using “String Object”)
The “String Object” refers to the set of characters that are created with the “new” keyword. In this example, it is used with the “valueOf()” method to return a string value.
JavaScript Code
This JavaScript code is the same as Example 1 but with a few amendments. Let’s go through it:
function demo() {
var text = new String("Welcome to Linuxhint");
var result = text.valueOf();
document.getElementById("sample").innerHTML = result;
}
</script>
In the above code snippet:
- The “demo()” function includes the “text” variable to specify the string object with the help of the “new” keyword and the “String()” constructor, respectively.
- After that, the “result” variable applies the “valueOf()” method to return the value of the String object.
Output
As seen, the desired requirement is fulfilled.
Conclusion
JavaScript offers the built-in String “valueOf()” method for getting the string values that may be numbers, strings, boolean, undefined, and symbols. It does not affect the original string and only displays the value in the form of a string. In addition, its returned value is equivalent to “Sprototypetotype.toString” for displaying the object as a text. This guide demonstrated the usage and implementation of the String “valueOf()” method in JavaScript.