Thursday, July 4, 2024
HomeLanguagesPhpJavaScript Number toString() Method

JavaScript Number toString() Method

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: 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.

Javascript




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. 

Javascript




let num=213;
console.log("Output : " + num.toString(8));


Output:

Output : 325

Example: Converting a number to a string with base 16, we will have to call the toString() method by passing 16 as a parameter. 

Javascript




let num=213;
console.log("Output : " + num.toString(16));


Output:

Output : d5

Example: 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




let num=213;
console.log("Output : " + num.toString());


Output:

Output :213

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

We have a complete list of Javascript Number Objects, to check those please go through this Javascript Number Complete reference article.

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments