JavaScript Number toString() Method
The JavaScript Number toString() Method in Javascript is used with a number and converts the number to a string. It is used to return a string representing the specified Number object.
Syntax:
num.toString(base)
Parameters Used: This method accepts a single optional parameter base. This parameter specifies the base in which the integer is represented in the string. It is an integer between 2 and 36 which is used to specify the base for representing numeric values.
Return Value: The num.toString() method returns a string representing the specified number object.
Below is an example of the Number toString() Method.
Example:
Javascript
<script type= "text/javascript" > var num=12; console.log( "Output : " + num.toString(2)); </script> |
Output:
Output:1100
Converting a number to a string with base 2: To convert a number to a string with base 2, we will have to call the toString() method by passing 2 as a parameter.
Javascript
<script type= "text/javascript" > var num=213; console.log( "Output : " + num.toString(2)); </script> |
Output:
Output:11010101
Converting a number to a string with base 8: To convert a number to a string with base 8, we will have to call the toString() method by passing 8 as a parameter.
Javascript
<script type= "text/javascript" > var num=213; console.log( "Output : " + num.toString(8)); </script> |
Output:
Output : 325
Converting a number to a string with base 16: To convert a number to a string with base 16, we will have to call the toString() method by passing 16 as a parameter.
Javascript
<script type= "text/javascript" > var num=213; console.log( "Output : " + num.toString(16)); </script> |
Output:
Output : d5
When no parameter is passed: If the toString() method is called without passing any parameter then the number will be converted to a string without change in BASE. Below is the program to illustrate this:
Javascript
<script type= "text/javascript" > var num=213; console.log( "Output : " + num.toString()); </script> |
Output:
Output :213
We have a complete list of Javascript Number Objects, to check those please go through this Javascript Number Complete reference article.
Supported Browsers:
- Google Chrome 1 and above
- Internet Explorer 3 and above
- Firefox 1 and above
- Apple Safari 1 and above
- Opera 4 and above
- Edge 12 and above
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Please Login to comment...