How To Convert Number To String In JavaScript

How To Convert Number To String In JavaScript? Data management is an essential responsibility for all programmers, and JavaScript is a very flexible language that provides numerous built-in methods to convert data into other data types. One of these ways is the toString() function. It is possible to change a variable’s data type from a number to a string using the toString() method. In this tutorial, we’ll discuss how to convert a number to a string in JavaScript.

When converting number to string in JavaScript, the toString() method is frequently utilized. It only requires one optional parameter. Following is the syntax of the toString() method.


The toString() method only accepts one optional parameter. The base of the number that will be included in the string is specified by this option.

Return Value: A string containing the supplied number object converted to the specified base is returned by the toString() method.

How To Convert Number To String In JavaScript Using toString()

If we merely wish to express the number in its string form without modifying its base, we only need to call the.toString() method without giving any parameters:

var num = 9856;
console.log(typeof num);

// convert number to string
var str = num.toString();
console.log(typeof str);



How to Convert a Number with Base 2 into a String (Binary) in JavaScript

We must also give an argument if we want to convert a number to a string and to a different base, let’s say base 2. We shall include 2 in this case since we want to convert the integer to binary:

var num = 8347;
var str = num.toString(2);



How To Convert Number To a Base-8 String (Octal) In JavaScript

We will enter 8 as a parameter to convert the integer to a string and a base 8 number:

var num = 449;
var str = num.toString(8);



Convert Number To a Base-16 String (HexaDecimal) In JavaScript

Similarly, we must call the toString() method and supply the parameter 16 to it in order to convert the number both into a HexaDecimal and into a string:-

var num = 904;
var str = num.toString(16);



What Other Data Types Can Be Used with the function toString() { [native code] }() Method?
Other data types can also be converted into their string equivalents using the.toString() method in addition to integers.

Let’s examine an illustration of how an array is transformed into its string representation:-

var arr = [ "Know", "Program" ];
var str = arr.toString();



An integer can be converted to a string using the toString() function. It just requires one optional parameter, which can be used to change the number’s base. In some circumstances, dealing with strings in JavaScript is much simpler than working with numbers, therefore changing a number into a string is not an absurd concept.

In this tutorial, we learned how to use JavaScript’s tostring() method to convert a number to String in JavaScript. Additionally, we discovered how to turn booleans and objects, as well as other data types, into strings.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *