The JavaScript valueOf function is a String function used to return the string. The syntax of the valueOf function is as shown below:
String_Object.valueOf()
JavaScript valueOf Example
The following set of examples will help you understand the valueOf function. It does not accept any arguments.
The last two JavaScript statements will return the value of the above-declared string variables str1 and str2. Next, we used the If-else Statement to check whether the value of the String variable Str1 is strictly equal to Str2 or not. For more functions, please refer to the JavaScript string functions article and the JavaScript If-else Statement available on the Learn JavaScript page.
<!DOCTYPE html>
<html>
<head>
<title> ValueOfJavaScript </title>
</head>
<body>
<h1> JavaScriptValueOf </h1>
<script>
var Str1 = "Tutorial Gateway";
var Str2 = "Learn JavaScript";
var Str3 = Str1.valueOf();
var Str4 = Str2.valueOf();
document.write(Str3 + "<br \>");
document.write(Str4 + "<br \>");
if (Str3 === Str4)
{
document.write(" Both are Equal <br \>");
}
else
{
document.write(" Both are Not Equal <br \>");
}
</script>
</body>
</html>

Related posts
- JavaScript substring
- JavaScript slice()
- split string in JavaScript
- JavaScript replace string
- JavaScript indexof