toString()
is a built-in
JavaScript method used to
convert values into string.
Let us take a look at an example:
In addition to toString()
,
JavaScript provides the String()
function,
which serves a similar purpose.
The main difference lies in how you use them:
toString()
is called on
an existing value.
For example - a number, boolean,
or object with a custom toString method.
It converts that specific value into a string.
String()
is a function to which
you can pass any value as a parameter,
and
it will transform that value into a string.
Here's an example of using String()
:
While both toString()
and
String()
can be used to convert
values to strings,
String()
is more versatile.
toString()
method has a few limitations,
we will cover those in the coming lessons.