Number.prototype.toExponential()
Formats a number in exponential notation with one digit before the decimal point.
toExponential()
toExponential(fractionDigits)Parameters
| Parameter | Type | Description |
|---|---|---|
$fractionDigitsoptional | number | How many digits to show after the decimal point. Must be between 0 and 100 inclusive. Omitted, it uses as many digits as are needed to represent the value uniquely. |
Return value
stringA string representing the number in exponential notation, with one digit before the decimal point and `fractionDigits` digits after it.
Errors
Throws a RangeError if fractionDigits is outside 0–100, and a TypeError if called on a value that is not a number.
toExponential() always produces the same shape: one digit, a decimal point, then the exponent — "7.71e+1". That predictability is the reason to use it. Values spanning many orders of magnitude line up in a column, and the number of digits you actually rely on is stated explicitly rather than implied by position.
When to reach for it
Physical constants, tolerances, and anything below 10-6 or above 1021, where toString() would flip to exponent form anyway but with a digit count you did not choose. It is also the honest way to display a value like Number.EPSILON, whose decimal expansion is unreadable.
The syntax trap
Writing 77.toExponential() is a syntax error: the parser reads the dot as the start of a decimal fraction. Use (77).toExponential(), or leave a space — 77 .toExponential(). This bites people writing quick console one-liners far more often than it bites them in real code, where the value is usually already in a variable.
Omitting the argument
With no fractionDigits, you get exactly as many digits as are needed to distinguish this double from its neighbours — no padding, no loss. Pass a number only when you want a fixed column width.
Example
When to use it
- Display a physics constant or tolerance in scientific notation
- Line up values spanning many orders of magnitude in a log column
- Show the exact scale of a very small floating-point difference
Frequently asked questions
Why is 77.toExponential() a syntax error?
The parser treats the dot after 77 as the start of a decimal fraction. Write (77).toExponential(), 77.0.toExponential(), or leave a space before the dot.
What does toExponential() return when I pass no argument?
It returns as many fraction digits as are needed to identify the value uniquely, so no precision is lost and no zeros are padded on.
How do I get just the exponent of a number?
Split the result on "e" and convert the second half: Number(n.toExponential().split('e')[1]). This is exact, unlike Math.log10 which can be off by one at exact powers of ten.
More examples
Aligning values across many magnitudes
Every row has the same mantissa shape, so the exponents form a readable column that plain decimal output could never produce for this range.
const readings = [0.0000042, 3.5, 980000, 1.6e-19];
for (const r of readings) {
console.log(r.toExponential(2).padStart(10));
}
// " 4.20e-6"
// " 3.50e+0"
// " 9.80e+5"
// " 1.60e-19"Pulling the exponent back out
Splitting the exponential string is an exact way to get the decimal order of magnitude, avoiding the floating-point rounding that Math.log10 can introduce at exact powers of ten.
function magnitude(n) {
const [, exp] = n.toExponential().split('e');
return Number(exp);
}
console.log(magnitude(1234)); // 3
console.log(magnitude(0.005)); // -3
console.log(magnitude(9.99)); // 0
console.log(magnitude(1)); // 0Comparing with toPrecision and toFixed
All four describe the same value: toExponential() always shows the exponent, toPrecision() only when the number is very small or very large, and toFixed() quietly discards the significant digits here.
const v = 0.000123456;
console.log(v.toExponential(3)); // "1.235e-4"
console.log(v.toPrecision(4)); // "0.0001235"
console.log(v.toFixed(4)); // "0.0001"
console.log(v.toString()); // "0.000123456"
Discussion