JS: Number.prototype.toString (Number to String)

By Xah Lee. Date: . Last updated: .

Number.prototype.toString

(number).toString()

Convert a number to string.

console.log((30).toString() === "30")
(number).toString(radix)

Use base radix.

// integer to binary string
console.log((3).toString(2) === "11");
// true

// decimal number to binary string
console.log((2.5).toString(2) === "10.1");
// true
// integer to hexadecimal
console.log((15).toString(16) === "f");
// true

// works for decimal number too
console.log((15.5).toString(16) === "f.8");
// true

💡 TIP: Auto Convert Number to String

JavaScript automatically convert number to string. Adding a string to number results string.

console.log(3 + "4" === "34");
// true

console.log(typeof (3 + "4") === "string");
// true

JavaScript. Convert Decimal, Hexadecimal, Binary