Example 1 — Decimal String
Convert a number to text with the default base 10.
(17).toString(); // "17" How It Works
With no argument, toString() uses base 10. Parentheses around 17 avoid a parser conflict with the decimal point.

Number.prototype.toString() turns a number into a string (same API as MDN Number.toString). Optional radix chooses base 2–36 for binary, octal, hex, and more. This tutorial covers syntax, negatives, scientific notation, five examples, and try-it labs.
toString(radix)
Base 10
2 to 36
Colors & bits
Keep −
Everywhere
Numbers are great for math; strings are great for display, URLs, and colors. toString() is the built-in bridge: (255).toString() is "255", and (255).toString(16) is "ff".
Unlike string concatenation alone, the optional radix argument lets you print the same value in many bases without writing your own converter.
This page is part of JavaScript Number Methods. Related formatters include toFixed(), toPrecision(), and toExponential().
toString() MethodCall number.toString() or number.toString(radix) on a number primitive or Number object. You get a string; the number itself does not change.
For radices above 10, letters a–z stand for digits 10–35. Hexadecimal (base 16) therefore uses a–f.
Integer literals need a space or parentheses before the dot: (17).toString() or 17..toString(). Otherwise 17.toString looks like a decimal to the parser.
General form of Number.prototype.toString:
number.toString()
number.toString(radix) 10.Always a string for the number in the chosen base. Special cases:
| Value | Returns |
|---|---|
| Normal number | Digits in the chosen radix (e.g. (254).toString(16) → "fe") |
| Negative number | Leading - plus the digits (e.g. (-10).toString(2) → "-1010") |
0 or -0 | "0" |
Infinity | "Infinity" |
NaN | "NaN" |
radix < 2 or > 36.this value (for example a plain object).(10).toString(); // "10"
(6).toString(2); // "110"
(254).toString(16); // "fe"
(-10).toString(2); // "-1010"
parseInt("ff", 16).toString(2); | Goal | Code |
|---|---|
| Decimal string | n.toString() |
| Binary | n.toString(2) |
| Hexadecimal | n.toString(16) |
| Hex channel (0–255) | Math.abs(c).toString(16) |
| Change base of a string | parseInt(s, from).toString(to) |
| Fixed decimals | n.toFixed(2) (not toString) |
Four facts to remember about Number.prototype.toString().
stringThe number is never changed
2–36Optional base for digits
10Decimal when radix is omitted
BaselineWorks in all modern engines
toString() vs toFixed() vs String()toString(radix) | toFixed(digits) | String(n) | |
|---|---|---|---|
| Main job | Any base 2–36 | Fixed decimals | Simple decimal text |
| Radix | Yes | No | No |
| Best for | Hex, binary, display | Currency / UI decimals | Quick coercion |
Examples follow MDN patterns. Use View Output or Try It Yourself for each case.
One call each — decimal, binary, and hex.
Convert a number to text with the default base 10.
(17).toString(); // "17" With no argument, toString() uses base 10. Parentheses around 17 avoid a parser conflict with the decimal point.
Pass radix 2 to print bits.
(6).toString(2); // "110" 6 in binary is 110 because 4 + 2 = 6.
Pass radix 16 for hexadecimal digits.
(254).toString(16); // "fe" Hex letters are lowercase (a–f). Use .toUpperCase() if you need FE.
Signs and converting between bases.
The leading minus sign is kept, even in binary.
(-10).toString(2); // "-1010" This is not two’s complement — you get a - plus the positive binary digits.
Parse a hex string, then print it in binary.
parseInt("FF", 16).toString(2); // "11111111" parseInt reads the hex text; toString(2) writes the same value as binary.
#rrggbb with toString(16).toString(2).parseInt(str, radix).toFixed or a decimal library.toString() Builds the StringMust be a Number primitive or wrapper.
Default 10, or your radix 2–36.
Letters for values above 9; optional -.
Original number unchanged.
RangeError.Object.prototype.toString; Number has its own algorithm.toLocaleString().Number.prototype.toString() is Baseline Widely available — part of JavaScript since early ECMAScript editions and supported in every modern browser and Node.js.
Safe for production everywhere. Optional radix 2–36 works consistently across engines.
Bottom line: Use freely. Prefer BigInt for huge integer base conversions that exceed Number.MAX_SAFE_INTEGER.
Number.prototype.toString() converts a number to text and unlocks base conversion with a single optional radix. It is ideal for hex colors, binary debugging, and everyday display strings.
Continue with toExponential() for scientific notation, explore Array toString() for list conversion, or return to the Number methods hub.
(255).toString(16)padStart(2, "0") for CSSBigInt for huge integer conversionstoFixed when you need exact decimal places10.toString() without space/parens (syntax error)Number when precision matterstoString for currency roundingNumber.toString()Turn numbers into text in any base from 2 to 36.
string
API2–36
Basea–f
DigitsKept
NegativesBaseline
SupportOverriding Number.prototype.toString affects Number objects, but number primitives are usually converted with the engine’s built-in algorithm — so `${1}` still yields "1" even after a prototype override, while `${new Number(1)}` can show your custom string.
Return to the hub for the growing Number.prototype collection.
8 people found this page helpful