JavaScript

How to Convert a Number into a String in JavaScript

Management of Data is a crucial task for any programmer; JavaScript is a very versatile programming language which offers multiple built-in methods to convert data into different data types. The .tostring() is one of these methods. The .tostring() method can be used to convert the data type of a variable from a number to a string. In this how-to guide we will learn to convert a number into a string using the .tostring() method in JavaScript.

The .tostring() method is commonly used in combination with numbers to convert them into strings. It takes a single optional parameter:

Syntax:

num.toString(base)

Parameter: The .tostring() method takes a single optional parameter. This parameter will specify the base of the number which will be stored in the string.

Return Value: The .tostring() method returns a string which contains the specified number object converted to the specified base.

How to change a number into a string

We just need to invoke the .toString() method without passing any parameters, if we only want to represent the number in its string form without changing its base:

var num = 549;

var str = num.toString();

console.log(num);

console.log(typeof num);

console.log(str);

console.log(typeof str);

How to convert a number to a string with base 2(binary)

If we want to convert a number into a string as well as to a different base, let’s say 2, then we need to pass a parameter as well. In this case we will pass 2 as we want to convert the number into binary:

var num = 549;

var str = num.toString(2);

console.log(str);

How to convert a number to a string with base 8(octal)

To convert the number into a string as well as into a base 8 number we will pass 8 as a parameter:

var num = 549;

var str = num.toString(8);

console.log(str);

How to convert a number to a string with base 16(HexaDecimal)

Similarly to convert the number into a HexaDecimal while also converting it into a string we need to invoke the .tostring() method and pass 16 as a parameter to it:

var num = 549;

var str = num.toString(16);

console.log(str);

How to Use Other Data Types with the toString() Method

The .toString() method is also used to change data types other than numbers into their string representations.

Let’s look at an example where an array has been converted into its string representation:

var arr = [ "John", "Jerry" ];

var str = arr.toString();

console.log(str);

Similarly we can convert any data type into a string:

var bool = true;

var str = bool.toString();

console.log(str);

console.log(typeof str);

Conclusion

The .toString() method takes a number and changes it into a string. It takes a single optional parameter which can be used to convert the number to a different base. In JavaScript working with strings is a lot easier than numbers in some cases so converting a number into a string is not an absurd idea.

In this guide we learned to convert a number into a string data type in JavaScript using the .tostring() method. Moreover we also learned to convert other data types such as objects and booleans into strings as well.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.