In this section, we will see what the toString() method is and how to use it in JavaScript.
JavaScript String toString() Method
The `toString()` method will return the primitive string value of the target `String` object.
Note: we can override this method in objects so when an object is invoked in an operation where string operands were needed, this method gets called automatically and hence the exact string value we’ve declared for the method will return.
JavaScript String toString() Syntax
string.toString()
String toString() Method Parameters:
The method does not take any argument.
String toString() Method Return Value:
The return value of this method is the primitive string value of the target string object.
Example: using String toString() method in JavaScript
const str = new String("Hello World!"); console.log(`${str}`); console.log(str.toString());
Output:
Hello World! Hello World!