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

JavascriptWeb DevelopmentFront End Technology

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
raja
Published on 03-Feb-2018 17:35:53
Advertisements