The toString function in JavaScript retrieves a string that represents the object it is called on. Essentially, this function offers a string depiction of the object and functions similarly to the valueOf method.
Syntax
The syntax representing the toString method is as follows:
Example
object.toString()
Returns
A String representing the object
JavaScript String toString Method Example
Let's explore a few basic illustrations demonstrating the utilization of the toString method.
Example 1
Here, we will print the value of string object.
Example
<script>
var str="Example";
document.writeln(str.toString());
</script>
Output:
Output
Example
Example 2
Let's explore another instance demonstrating how to display the value of a string object.
Example
<script>
var str=new String("Example")
document.writeln(str.toString());
</script>
Output:
Output
Example