JavaScript Number toString() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Base 2–36

What You’ll Learn

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.

01

Syntax

toString(radix)

02

Default

Base 10

03

Radix

2 to 36

04

Hex / bin

Colors & bits

05

Negatives

Keep −

06

Baseline

Everywhere

Introduction

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().

Understanding the toString() Method

Call 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 az stand for digits 10–35. Hexadecimal (base 16) therefore uses af.

💡
Beginner Tip

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.

📝 Syntax

General form of Number.prototype.toString:

JavaScript
number.toString()
number.toString(radix)

Parameters

  • radix (number, optional) — integer from 2 to 36. Default 10.

Return value

Always a string for the number in the chosen base. Special cases:

ValueReturns
Normal numberDigits in the chosen radix (e.g. (254).toString(16)"fe")
Negative numberLeading - plus the digits (e.g. (-10).toString(2)"-1010")
0 or -0"0"
Infinity"Infinity"
NaN"NaN"

Exceptions

  • RangeErrorradix < 2 or > 36.
  • TypeError — called on a non-Number this value (for example a plain object).

Common patterns

JavaScript
(10).toString();           // "10"
(6).toString(2);           // "110"
(254).toString(16);        // "fe"
(-10).toString(2);         // "-1010"
parseInt("ff", 16).toString(2);

⚡ Quick Reference

GoalCode
Decimal stringn.toString()
Binaryn.toString(2)
Hexadecimaln.toString(16)
Hex channel (0–255)Math.abs(c).toString(16)
Change base of a stringparseInt(s, from).toString(to)
Fixed decimalsn.toFixed(2) (not toString)

🔍 At a Glance

Four facts to remember about Number.prototype.toString().

Returns
string

The number is never changed

Radix
2–36

Optional base for digits

Default
10

Decimal when radix is omitted

Support
Baseline

Works in all modern engines

📋 toString() vs toFixed() vs String()

toString(radix)toFixed(digits)String(n)
Main jobAny base 2–36Fixed decimalsSimple decimal text
RadixYesNoNo
Best forHex, binary, displayCurrency / UI decimalsQuick coercion

Examples Gallery

Examples follow MDN patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

One call each — decimal, binary, and hex.

Example 1 — Decimal String

Convert a number to text with the default base 10.

JavaScript
(17).toString();  // "17"
Try It Yourself

How It Works

With no argument, toString() uses base 10. Parentheses around 17 avoid a parser conflict with the decimal point.

Example 2 — Binary String

Pass radix 2 to print bits.

JavaScript
(6).toString(2);  // "110"
Try It Yourself

How It Works

6 in binary is 110 because 4 + 2 = 6.

Example 3 — Hex String

Pass radix 16 for hexadecimal digits.

JavaScript
(254).toString(16);  // "fe"
Try It Yourself

How It Works

Hex letters are lowercase (af). Use .toUpperCase() if you need FE.

📈 Practical Patterns

Signs and converting between bases.

Example 4 — Negative Numbers

The leading minus sign is kept, even in binary.

JavaScript
(-10).toString(2);  // "-1010"
Try It Yourself

How It Works

This is not two’s complement — you get a - plus the positive binary digits.

Example 5 — Change Base

Parse a hex string, then print it in binary.

JavaScript
parseInt("FF", 16).toString(2);  // "11111111"
Try It Yourself

How It Works

parseInt reads the hex text; toString(2) writes the same value as binary.

🚀 Common Use Cases

  • UI labels — turn scores and counts into text for the DOM.
  • CSS / canvas colors — build #rrggbb with toString(16).
  • Debugging bits — print flags with toString(2).
  • Base conversion — pair with parseInt(str, radix).
  • Logging — readable snapshots without locale grouping.
  • Not for money decimals — use toFixed or a decimal library.

🧠 How toString() Builds the String

1

Read the number

Must be a Number primitive or wrapper.

Input
2

Choose the base

Default 10, or your radix 2–36.

Radix
3

Encode digits

Letters for values above 9; optional -.

Encode
4

Return string

Original number unchanged.

📝 Notes

  • Does not change the number — returns a new string.
  • Radix outside 2–36 throws RangeError.
  • Large / tiny decimals in base 10 may use scientific notation.
  • Floating-point rounding can surprise you on huge integers — see MDN’s precision note.
  • Overrides Object.prototype.toString; Number has its own algorithm.
  • For locale grouping (thousands separators), use toLocaleString().

Browser & Runtime Support

Number.prototype.toString() is Baseline Widely available — part of JavaScript since early ECMAScript editions and supported in every modern browser and Node.js.

Baseline · Widely available

Number.prototype.toString()

Safe for production everywhere. Optional radix 2–36 works consistently across engines.

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Number.toString() Excellent

Bottom line: Use freely. Prefer BigInt for huge integer base conversions that exceed Number.MAX_SAFE_INTEGER.

Conclusion

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.

💡 Best Practices

✅ Do

  • Use parentheses for integer literals: (255).toString(16)
  • Pad hex channels with padStart(2, "0") for CSS
  • Validate radix stays between 2 and 36
  • Use BigInt for huge integer conversions
  • Prefer toFixed when you need exact decimal places

❌ Don’t

  • Write 10.toString() without space/parens (syntax error)
  • Expect two’s-complement binary for negatives
  • Parse huge hex with Number when precision matters
  • Use toString for currency rounding
  • Pass radix 0, 1, or 37+

Key Takeaways

Knowledge Unlocked

Five things to remember about Number.toString()

Turn numbers into text in any base from 2 to 36.

5
Core concepts
🔢 02

Radix

2–36

Base
🔸 03

Hex

a–f

Digits
04

Sign

Kept

Negatives
05

Safe

Baseline

Support

❓ Frequently Asked Questions

Number.prototype.toString() returns a string representing the number. With no argument it uses base 10. Pass an optional radix from 2 to 36 to print the number in that base (for example 2 for binary, 16 for hexadecimal).
radix is an integer from 2 through 36 that sets the numeric base. Digits above 9 use letters a–z (for example hex uses a–f). If you omit radix, JavaScript uses 10. Values outside 2–36 throw a RangeError.
The leading minus sign is kept. Even in binary, you get a string like "-1010" — not two's complement. Both 0 and -0 become "0".
For radix 10 only, if the magnitude is >= 1e21 or < 1e-6, the string uses exponential form such as "3.1622776601683794e+21". Other radices use a normal digit expansion.
toString() picks a short distinguishing representation and can use scientific notation. toFixed() and toPrecision() let you control decimal places or significant digits and can be more precise for display. Use String(n) or n + "" for a simple decimal string without calling the method explicitly.
It is part of the ECMAScript language from early editions and is Baseline Widely available — safe in every modern browser and Node.js.
Did you know?

Overriding 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.

More Number Methods

Return to the hub for the growing Number.prototype collection.

Number methods hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful