javascript
  1. javascript-tostring

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.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ToString Example</title>
</head>
<body>

<script>
    // Example variable
    const x = 10;

    // Convert the variable to a string and log the result
    document.write("<p>Converted to string: " + x.toString() + "</p>");
</script>

</body>
</html>

Try Playground

Explanation

The toString() method in JavaScript takes the value of any given object and converts it to a string. This method is commonly used to convert numeric data types to strings so that they can be concatenated with other strings. The toString() method converts other data types, such as objects and dates, to their corresponding string representations.

Use

Here are some of the common uses of the toString() method in JavaScript:

  • To convert a numeric value to a string before concatenating it with another string.
  • To output the value of an object or an array or convert it to a string format.
  • To convert the date object to a string in ISO standard format.

Important Points

Here are some important points related to toString() method in JavaScript:

  • The toString() method is inherited by all JavaScript objects from the Object prototype.
  • If the radix parameter is not specified, the default value is 10, which means that the number is converted to a decimal value.
  • The output of the toString() method depends on the data type of the value that is being converted.

Summary

The toString() method in JavaScript is a powerful method that can convert different types of data to strings. This method is useful when converting numbers to strings for display purposes or when working with string manipulation. It's important to keep in mind that the output of the toString() method depends on the data type of the value being converted.

Published on: