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

By Xah Lee. Date: . Last updated: .

Number.prototype.toString

(number).toString()

Convert a number to string.

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

Use base radix.

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

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

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

Implicit conversion of number to string

🛑 warning: Adding a string to number results string.

console.assert(3 + "4" === "34");
console.assert(typeof (3 + "4") === "string");