When you need to show a number in scientific notation like 1.234e+6
, JavaScript gives you Number.prototype.toExponential()
. It returns a string, leaving your original number untouched. You control how many digits appear after the decimal point, and JavaScript takes care of rounding.
toExponential()
toExponential(fractionDigits)
Call it on any number (literal or Number
object). With no argument, it uses as many digits as needed to uniquely represent the value. With fractionDigits
, it rounds to that many digits after the decimal point and always uses exponential notation. If fractionDigits
is not between 0
and 100
inclusive, JavaScript throws a RangeError
. The method is not generic—the receiver must be a Number (or a Number
object), otherwise you get a TypeError
.
(12345).toExponential(); // "1.2345e+4"
(12345).toExponential(2); // "1.23e+4"
(0.0012345).toExponential(); // "1.2345e-3"
(0.0012345).toExponential(1);// "1.2e-3"
Those strings are formatted with exactly one digit before the decimal point and an exponent that shifts the decimal point accordingly.
Rounding behavior and precision
toExponential(f)
rounds to f
digits after the decimal point using the same rounding rules as toFixed
. The rounding is applied to the significand (the part before the e±…
). For example, 9.995.toExponential(2)
becomes "1.00e+1"
, because the significand rounds up and bumps the exponent.
(9.995).toExponential(2); // "1.00e+1"
(9.994).toExponential(2); // "9.99e+0"
(999_500).toExponential(2);// "1.00e+6"
If you omit fractionDigits
, JavaScript picks “as many digits as necessary” to uniquely identify the value—handy when you just want exponential form without worrying about precision.
Special values: zero, Infinity, NaN
- Finite numbers are formatted as described above.
- For non-finite values (
Infinity
,-Infinity
, andNaN
), the spec says to fall back to the usual number stringification. That meansInfinity.toExponential()
returns"Infinity"
andNaN.toExponential()
returns"NaN"
. - Zero comes out as
"0e+0"
(with rounding zeros if you specify digits).
Infinity.toExponential(); // "Infinity"
NaN.toExponential(); // "NaN"
(0).toExponential(4); // "0.0000e+0"
Numeric literal gotcha
If you call a method directly on an integer literal, the dot may be parsed as a decimal point. Write either a second dot or wrap the number in parentheses:
// Bad: the parser reads `10.` as a literal with a decimal point
// 10.toExponential(); // SyntaxError
(10).toExponential(); // "1.0e+1"
10..toExponential(); // "1.0e+1" (two dots)
This parsing nuance applies to any method call on an integer literal.
Choosing between toExponential, toFixed, and toPrecision
- Use
toExponential
when you want scientific notation specifically. - Use
toFixed
when you want a fixed number of digits after the decimal in normal notation. - Use
toPrecision
when you want a fixed total number of significant digits; it may choose fixed or exponential notation depending on the magnitude.
const x = 12345.6789;
x.toExponential(3); // "1.235e+4" (exponential, 3 digits after decimal)
x.toFixed(3); // "12345.679" (fixed-point, 3 digits after decimal)
x.toPrecision(3); // "1.23e+4" (3 significant digits)
Error conditions and type requirements
toExponential
throws:
// RangeError if fractionDigits is out of range
(1).toExponential(101);
// TypeError if called with a non-Number receiver
Number.prototype.toExponential.call("not a number", 2);
The valid fractionDigits
range is 0
–100
in current ECMAScript editions. Historically, some older engines only guaranteed 0
–20
, so if you’re targeting very old browsers, be cautious. Also, toExponential
is a Number method; BigInts don’t have it, and mixing BigInt with Number causes a “can’t convert BigInt to number” error.
Practical formatting patterns
Show values compactly in UIs
function compactScientific(n) {
// Show 2 or 3 decimals depending on magnitude
const decimals = Math.abs(n) >= 1 ? 2 : 3;
return n.toExponential(decimals);
}
compactScientific(1500000); // "1.50e+6"
compactScientific(0.0002345); // "2.345e-4"
This is handy when you need consistent width columns or logs that prefer scientific notation for both very large and very small values.
Preserve significant digits with toPrecision
first
If your goal is “about N significant digits” but in scientific form, combine toPrecision
and a quick check:
function sigExp(n, sig = 6) {
const s = n.toPrecision(sig); // may be fixed or exponential
return /e/i.test(s) ? s : Number(s).toExponential(sig - 1);
}
sigExp(987654321, 4); // "9.877e+8"
sigExp(0.00098765, 4); // "9.877e-4"
toPrecision
already uses exponential format for very large/small magnitudes; if it didn’t, we coerce and format explicitly.
Frequently asked nuances
Why a string, not a number?
The output is a string because it’s a textual representation. If you need the numeric value again, wrap with Number(...)
or unary +
:
const s = (123).toExponential(1); // "1.2e+2"
+s; // 120 (number again)
What about localization?
toExponential
is locale-agnostic and always uses ASCII digits and e±
. If you need localized formatting, use Intl.NumberFormat
with notation: 'scientific'
. The core behavior here is standardized; toExponential
itself doesn’t localize.
Does it work on Number
objects?
Yes. Primitive numbers are temporarily “boxed” and use the same prototype method; new Number(42).toExponential(3)
works. But remember: the method is not generic—its this
must wrap a Number.
Historical limits
Older specifications only guaranteed 0–20
for fractionDigits
. Modern ECMAScript specifies 0–100
. If you’re working in truly ancient environments (e.g., legacy IE), limits and edge behavior might differ.
References
Source | What it covers |
---|---|
MDN: Number.prototype.toExponential() | Purpose, syntax, parameters, exceptions, examples, numeric-literal note |
ECMAScript 2023, §21.1.3.2 Number.prototype.toExponential | Normative algorithm, fractionDigits range 0–100 , non-finite handling |
ECMAScript 2026 draft, Numbers and Dates intro | Methods of Number.prototype are not generic (receiver must be a Number) |
MDN: Number.prototype.toFixed() | Rounding semantics reused by toExponential |
MDN: Number.prototype.toPrecision() | Significant-digits formatting and when exponential form is chosen |
MDN: Number (overview) | Number fundamentals and links to related instance methods |
MDN Error: “can’t convert BigInt to number” | BigInt/Number mixing pitfalls relevant to formatting numbers |
Microsoft ES3 notes: Number.prototype.toExponential | Historical note about older engines’ fractionDigits range |