In this post, we will learn,6 ways to convert int to string in JavaScript.The numbers in programming language sometimes are represented as “12”.Its data type is not a number but a string.
1. Tosting() to convert int to string in JavaScript
The tostring() method takes a number or float as input and converts it into a string. It returns a string representation of the number. It takes an optional base parameter.
The tostring() throw type errors while converting undefined and null to string.
Syntax
tostring(base)
Base: this is an optional parameter that makes converts an input number to another base. It is an int between 2 to 36.
JS Program to Convert number to a string
In this example, we are converting num, float, NAN, null, symbol, and undefined to string. But while converting null, undefined it throws typeerror as shown in the output.
const intnum = 50;
const floatnum = 45.50;
const num = 10;
const num_nan = NaN;
const symbol_val = Symbol('234');
const undefined_val = undefined;
const null_val = null;
console.log(intnum.toString());
console.log(floatnum.toString()) ;
console.log('num to string base 2:',num.toString(2))
console.log(num_nan.toString());
console.log(symbol_val.toString());
console.log(null_val.toString());
console.log(undefined_val.toString());
Output
"50"
"45.5"
"num to string base 2:", "1010"
"NaN"
"Symbol(234)"
TypeError: Cannot read property 'toString' of null
TypeError: Cannot read property 'toString' of undefined
2. Template Literal to to convert int to string in JavaScript
Template literal uses back-ticks(“) to define a string, which is introduced in ES6. Earlier we used to create a string by using single quotes (‘) or double quotes (“) quotes to define a string.
It throws a type error while converting a symbol type value to a string.
Let us understand with the below example how to convert the value to a string.
Example : Converting a value to string using Template String
intnum = 50;
int_to_str = `${intnum}`
console.log(int_to_str) ;
floatnum = 45.50;
console.log(`${floatnum}`) ;
num_nan = NaN;
console.log(`${num_nan}`);
Output
"50"
"45.5"
"NaN"
3. string () to convert int to string in JavaScript
The string() method allows us to create a primitive string type for the number passed to it as an argument. The string() method handles the NULL and undefined. It does not throw errors when NULL and undefined is passed.
JS Program to convert int to string
intnum = 50;
floatnum = 45.50;
num_nan = NaN;
let b = null;
var c ;
console.log(String(intnum));
console.log(String(floatnum));
console.log(String(num_nan));
console.log(String(b));
console.log(String(c));
Output
"50"
"45.5"
"NaN"
"null"
"undefined"
4. concatinating empty string
This is the naive way to convert a value to a string, number is concatenated with an empty string to convert. It is the fastest way to accomplish as compared to another way.
it throws a typeerror while converting a symbol type value to a string same as template literal.
Example : Converting a value to string using string() method
float = 45.50;
floatnum_to_str = ''+float;
num_nan = NaN;
nan_tostr = ''+num_nan;
console.log(num_to_str);
console.log(String(floatnum_to_str));
console.log(String(nan_tostr));
Output
"50"
"45.5"
"NaN"
5.JSON.Stringfy() to to convert int to string
JSON.Stringfy() method converts an object or value to a string.
JS Program to convert int to string
intnum = 50;
floatnum = 45.50;
let b = null;
var c ;
console.log(JSON.stringify(intnum));
console.log(JSON.stringify(floatnum));
console.log(JSON.stringify(b));
console.log(JSON.stringify(c));
Output
"50"
"45.5"
"null"
undefined
6.Using tofixed() method
The tofixed() method used to convert a number to a string. It rounds the number to specified number of the decimal digits.
Syntax
number.tofixed(digits)
Parameter
Digits: an optional parameter. It specified the number of digits after the decimal place.
Convert value to string tofixed() method
In this example, we are converting a number to a string by not passing any argument to tofixed() and passing a digit
intnum = 50;
floatnum = 45.503;
console.log(intnum.toFixed());
console.log(floatnum.toFixed());
console.log(floatnum.toFixed(2));
Output
"50"
"46"
"45.50"
Summary
We have explored 6 ways to convert int to string in JavaScript. List of functions we have used
- Tosting() Method :The tostring() method takes a number or float as input and converts it into a string
- Using Template Literal :It uses back-ticks(“) to define a string is introduced in ES6.
- String() Method :The string() method allows us to create a primitive string type for the number passed to it as an argument.
- By concatinating empty string : number is concatenated with an empty string to convert
- Using JSON.Stringfy() : convert number to string
- Using tofixed() method : convert given number to string.