Before getting into depth, we recommend a few packages that assist in executing the JavaScript files.
Prerequisites
The following set of prerequisites are recommended to carry out the operations specified in this guide:
– Latest version of Visual Studio Code
– Either the latest version of Nodejs or you can use your browser to test the code
Navigate to nodejs.org to download the latest available version
How the toString() method works in JavaScript
The functionality of any method or function depends on the syntax followed by that method/function. The syntax of the toString() method of JavaScript is provided below:
A variable consisting of a numerical value can be passed and it must be placed at <variable> location of the above-mentioned syntax.
Moreover, .toString() method can be used on a number directly (in place of <number>) by using the following syntax:
The .toString() method accepts a single argument named as base that is used to convert the number to a specific base and then it is converted to string. The base can be 2 (to convert to binary numbers), 8(for octal number), 10(for decimal number), or 16(for hexadecimal numbers):
How to convert Numbers to strings in JavaScript
This section provides a few examples which exercise the use of the .toString() method of JS. So, let’s start it:
Example 1
Firstly, we have created a .js file in Visual Studio Code and named it as numtostr.js. The code written inside is written and described below:
Code
console.log(typeof(n));
var st = n.toString();
console.log(typeof(st));
– A number (99) is stored in a variable named n
– The second line of the code will print the type of variable n
– Another variable (st) is used to store the value of .toString() method (that is applied on n variable)
– For verification, we have obtained the datatype of the st variable as well using the console.log(typeof(st)) method
Image of the Code and Output
Example 2: Passing number to .toString() method
A number can directly be passed to the .toString() method. For instance, the code provided below passes the number 10 to the .toString() method. The description of the code is shown below:
– A variable str is declared which stores the number 10 applied on .toString() method
– console.log method is used to get the type of the variable
Code
console.log(typeof(str));
Image of code and its output
Example 3: converting a “number to binary” and “then to a string”
You can use the .toString() method to convert a number into a binary and then to a string. To do so, the following code converts n variable to an equivalent binary number and then to a string:
Code
console.log(typeof(n)); //getting the type of n
var st = n.toString(2); //converting n to binary and then to string
console.log(st); //printing the st variable
console.log(typeof(st)); //getting the type of st variable
Note: The text written after “//” is considered as comments.
Image of code and output
Example 4: Converting “number to octal” and “then to string”
A number can be converted to octal base number and then to a string by using .toString() method. A new variable is created named as oct and is passed to .toString(8) method to convert it to octal and string simultaneously.
console.log(typeof(oct)); //getting the type of oct variable
var st = oct.toString(8); //converting oct to octal and then to string
console.log(st); //printing the st variable
console.log(typeof(st)); //getting the type of st variable
Image of code and output
Similarly, as in Example 4 and Example 5, the .toString(10) and .toString(16) methods convert the number to decimal and hexadecimal numbers respectively. Moreover, it will convert those numbers to string as well.
Conclusion
The .toString() method of JavaScript is used to convert numbers of various categories to a string value. This article provides a demonstration to convert numbers to strings in JavaScript. By going through this guide, you have learned the generic working mechanism of the .toString() method. Moreover, for better understandings, several examples are provided that exercise the .toString() method.