What is the best way to convert a number to a string in JavaScript?



The best way to convert a number to a string is using the toString() method, with different bases. The method has an optional parameter “radix”, which is used as a base (2, 8, 16) for a numeric value.

Example

You can try to run the following code to convert a number to a string −

Live Demo

<!DOCTYPE html> <html>    <body>       <script>          var num = 10;          var val1 = num.toString();          var val2 = num.toString(2);          var val3 = num.toString(8);          var val4 = num.toString(16);          document.write(val1 + "<br>" + val2 + "<br>" + val3 + "<br>" + val4);       </script>    </body> </html>

Output

10 1010 12 a
Updated on: 2020-06-17T06:37:14+05:30

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements