AppDividend
Latest Code Tutorials

How to Convert JavaScript Int to String

The toString() method in Javascript is used with the number and converts a number to a string. The toString() function returns the string representing the specified Number object.

Javascript int to string

To convert Javascript int to string, use the Number.toString() method. The toString() is a built-in JavaScript Number method that converts a number to a string. The toString() method accepts a radix argument, an optional argument.

Syntax

number.toString(base)

Parameters

The toString() function accepts a single optional parameter base. The base parameter defines the base in which the integer is represented in the string.

Return Value

The number.toString() function returns a string representing the specified Number object.

Example

See the below code.

// app.js

let a = 11;
let b = 11.00;
let c = 11.21;
let d = 19;

let opA = a.toString(2);
let opB = b.toString(2);
let opC = c.toString(2);
let opD = d.toString(2);

console.log(opA, typeof (opA));
console.log(opB, typeof (opB));
console.log(opC, typeof (opC));
console.log(opD, typeof (opD));

Output

1011 string
1011 string
1011.00110101110000101000111101011100001010001111011 string
10011 string

You can see that we converted int to string in the base of 2.

The num.toString() is really fast and better than + concatenation.

Converting an integer to a string with base 8

To convert an integer to a string with base 8, use the toString() method by passing 8 as a parameter.

// app.js

let a = 11;
let b = 11.00;
let c = 11.21;
let d = 19;

let opA = a.toString(8);
let opB = b.toString(8);
let opC = c.toString(8);
let opD = d.toString(8);

console.log(opA, typeof (opA));
console.log(opB, typeof (opB));
console.log(opC, typeof (opC));
console.log(opD, typeof (opD));

Output

13 string
13 string
13.1534121727024366 string
23 string

After changing the base, you can see that our output is now different.

Converting an integer to a string with base 16

To convert an integer to string with base 16, use the toString() method by passing 16 as a parameter.

let a = 11;
let b = 11.00;
let c = 11.21;
let d = 19;

let opA = a.toString(16);
let opB = b.toString(16);
let opC = c.toString(16);
let opD = d.toString(16);

console.log(opA, typeof (opA));
console.log(opB, typeof (opB));
console.log(opC, typeof (opC));
console.log(opD, typeof (opD));

Output

b string
b string
b.35c28f5c28f6 string
13 string

Converting using + concatenation

This is unsuitable as it is not performance effective, but I will show you how to convert a number to a string using a concatenation operator.

// app.js

let data = 11;
let app = '' + data;

console.log(app);
console.log(typeof(app));

Output

11
string

You can see that 11 is a string from an integer using + operator.

Javascript typeof() function is used to check the data type of any variable.

Conclusion

To convert int to string in Javascript, use the Number.toString() method.

Related posts

JavaScript string to int

JavaScript string search

JavaScript isInteger()

JavaScript parseInt()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.