JavaScript Number toString() Method
-
Syntax of JavaScript Number
toString()
Method -
Example 1: Use the
number.toString()
Method With Different Numbers -
Example 2: Use the
number.toString()
Method With Differentradix
-
Example 3: Use the
number.toString()
Method With Non-Decimal Number Values - Exceptions
-
Example 4: Use the
number.toString()
Method Withradix
Not Between 2 and 36
In JavaScript, the number.toString()
method is a built-in Number
library method, and users can invoke it on any number value by taking reference of the particular number variable or value.
The number.toString()
method converts the number value to a string value with a different base. Users can convert the decimal number value to a string with a different base between 2 and 36.
Syntax of JavaScript Number toString()
Method
let num = 20;
Number.toString();
Number.toString(radix);
Parameters
radix
- The radix
is an optional parameter, which takes the values between 2 and 36 and represents the base of the string.
Return
The number.toStirng()
method returns the string of the Number
objects with the radix
base.
Example 1: Use the number.toString()
Method With Different Numbers
In the example below, we use the number.toString()
method to convert the different positive and negative numbers to strings with the default base of 10.
Also, we have checked the type of the numValue1
variable before using the number.toString
method, giving the number
as an output.
The type of the numValue1
variable changes to a string
that users can see in the output after using the number.toString()
method.
let numValue1 = 20;
let numValue2 = -110;
console.log(typeof numValue1);
let str1 = numValue1.toString();
let str2 = numValue2.toString();
console.log(str1);
console.log(str2);
console.log(typeof str1);
Output:
number
20
-110
string
Example 2: Use the number.toString()
Method With Different radix
In this section, we will use the radix
parameter with the number.toString()
method. When we use the radix
values between 2 and 36, the method converts the decimal number to a string with base radix
and returns the string.
In the example below, we have converted one decimal value to a string with base 2, which means decimal to binary conversion. Also, we have converted other number values to octal and hexadecimal strings, respectively.
Users can see the output’s binary, octal, and hexadecimal strings of different decimal numbers.
let numValue1 = 50;
let numValue2 = 4;
let num = 30;
let str1 = numValue1.toString(2);
let str2 = numValue2.toString(8);
console.log(str1);
console.log(str2);
console.log(num.toString(16));
Output:
110010
4
1e
Example 3: Use the number.toString()
Method With Non-Decimal Number Values
We can only convert the decimal values to strings using the number.toString()
method. If we have a number string with a different base, we must first convert it to decimal values using the parseInt(number, base)
method.
The parseInt()
method takes two parameters. The first parameter is the string with a different base, and the second is the base
of the string.
It converts from the number string with a different basis to a decimal value, and after that, we can apply the toString()
method to a decimal value.
We have taken binary and hexadecimal strings in the example below and parsed their integer value using the parseInt()
method. After that, we invoked the number.toString()
method on the integer value.
let numValue1 = "10101";
let numValue2 = "A1";
let str1 = parseInt(numValue1,2).toString();
let str2 = parseInt(numValue2,16).toString();
console.log(str1);
console.log(str2);
Output:
21
161
Exceptions
RangeError
- If we use radix
outside the [2,36]
as a parameter value, the method will generate an exception and throw a RangeError
.
Example 4: Use the number.toString()
Method With radix
Not Between 2 and 36
When we use the radix
, which is not between 2 and 36, it raises the exception called the RangeError
. Here, we have used 0 and 37 as radix
values to convert the number to a string, and users can see what error it gives in the output.
let numValue1 = 200;
let numValue2 = 4310;
let str1 = numValue1.toString(37);
let str2 = numValue2.toString(0);
console.log(str1);
console.log(str2);
Output:
RangeError: toString() radix argument must be between 2 and 36
This article has seen the different examples and what exception occurs using the number.toString()
method. Generally, programmers can use the Number.toString()
method to change the base of number values and convert it to string.