JavaScript toString()
The toString()
method in JavaScript is used to convert any given value to a string. This method converts the value of an object to a string and returns the result. The method can be applied to most JavaScript data types such as numbers, arrays, objects, and dates.
Syntax
The syntax for using the toString()
method is as follows:
value.toString([radix])
value
: The value to be converted to a string.radix
: An optional parameter, indicating the base of the number in value. This is an integer value between 2 and 36.
Example
Consider the following example:
const x = 10;
console.log(x.toString()); // Outputs: "10"
In this example, 10
is a numeric value, but the toString()
method convert this to a string, and the output will be "10"
.
Output
The toString()
method returns a string representing the given value as follows:
- For numbers, the number will be converted to a string.
- For arrays, the array will be converted to a comma-separated string.
- For objects, the output string will be
[object Object]
. - For dates, the date will be converted to a string in the ISO standard format.