JavaScript Number toString() Method
The toString() method in JavaScript returns a number as a string. It allows converting numerical values to string representations, enabling operations like formatting output, displaying numbers in various formats, and facilitating string manipulation based on numeric values.
Syntax:
num.toString(base)
Parameters: This method accepts a single optional parameter
- base: 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 1: Converting a number to a string with base 2, we will have to call the toString() method by passing 2 as a parameter.
let num=213;
console.log("Output : " + num.toString(2));
Output:
Output:11010101
Example 2: Converting a number to a string with base 8, we will have to call the toString() method by passing 8 as a parameter.
let num=213;
console.log("Output : " + num.toString(8));
Output:
Output : 325
Example 3: Converting a number to a string with base 16, we will have to call the toString() method by passing 16 as a parameter.
let num=213;
console.log("Output : " + num.toString(16));
Output:
Output : d5
Example 4: 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.
let num=213;
console.log("Output : " + num.toString());
Output:
Output :213
Supported Browsers:
We have a complete list of Javascript Number Objects, to check those please go through this Javascript Number Complete reference article.