Below is the example of the Number toString() Method.
- Example:
<scripttype="text/javascript">var num=12;document.write("Output : " + num.toString(2));</script>chevron_rightfilter_none - Output:
Output:1100
The 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)
The toString() method is used with a number num as shown in above syntax using the ‘.’ operator. This method will convert num to a string.
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 are some examples to illustrate the working of toString() method in JavaScript:
-
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.
<scripttype="text/javascript">var num=213;document.write("Output : " + num.toString(2));</script>chevron_rightfilter_noneOutput:
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.
<scripttype="text/javascript">var num=213;document.write("Output : " + num.toString(8));</script>chevron_rightfilter_noneOutput:
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.
<scripttype="text/javascript">var num=213;document.write("Output : " + num.toString(16));</script>chevron_rightfilter_noneOutput:
Output : d5
-
When no parameter is passed: If the toString() method is called without passing any parameter then the number will be converted to string without change in BASE. Below is the program to illustrate this:
<scripttype="text/javascript">var num=213;document.write("Output : " + num.toString());</script>chevron_rightfilter_noneOutput:
Output :213
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Apple Safari
- Opera


