In Javascript, we can use the toString
method to get the string representation of an object.
Note: This function is equivalent to the
valueOf()
method.
toString();
This function doesn’t need any parameters.
This function will return a string that represents a String
object.
Note: every string in JavaScript is an object as well.
var ourstring = new String("Hello, Educative!");let int1 = BigInt(234);console.log(ourstring, int1);console.log(ourstring.toString(), int1.toString());console.log("Hello, Educative!".toString()); // a string is also a string objectconsole.log("Hello, Educative!".toString() == ourstring.valueOf()); // these methods are equivalent
String
object with the value "Hello, Educative!"
is created and a BigInt
with a value of 234
.ourstring
and int1
as their respective objects and then their string representation, using toString()
.toString()
is equivalent to using valueOf()
.