In this blog post, we will explore the toString() method in JavaScript strings. This method allows us to obtain the string representation of a String object.
Usage
The toString() method is invoked on a String object and returns the string representation of that object. Here is an example demonstrating its usage:
const str = new String('Test');
str.toString(); // 'Test'
As you can see, calling toString() on the str
object returns the string “Test”.
Equivalent to valueOf()
It’s important to note that the toString() method is equivalent to the valueOf() method in JavaScript strings. They both return the same result.
Conclusion
The toString() method in JavaScript strings is a handy way to obtain the string representation of a String object. It is equivalent to the valueOf() method and returns the same value.