CKTIS / JavaScript
Number.prototype.toExponential: exponential notation
toExponential returns exponential text with one digit before the decimal point and a requested number of digits after it.
Version: ECMAScript 3 onward; example target: Node.js 24.22.2
Syntax
number.toExponential(fractionDigits)number.toExponential(fractionDigits) formats a Number as decimal text with a lowercase e, an explicit exponent sign, and one digit before the decimal point. When fractionDigits is undefined, it chooses the shortest digit sequence needed to identify the Number uniquely. When supplied, the result has exactly that many digits after the decimal point.
The argument undergoes ToIntegerOrInfinity: fractions truncate toward zero and NaN becomes 0. For a finite receiver, the allowed range is 0..100; values outside it and ±Infinity throw RangeError. The argument is converted first, however, and a non-finite receiver then returns ordinary text before range validation, so Infinity.toExponential(101) yields "Infinity"; an argument object can still throw during conversion.
The method has existed since ECMAScript 3, is not generic, and throws TypeError for a receiver other than a Number or boxed Number. It rounds to the nearest mathematical result, choosing the larger magnitude on a tie after separating the sign; binary representation can therefore surprise. Negative zero is formatted without a minus sign.
Examples
console.log((1234.5).toExponential(2), (0.00125).toExponential(2));
console.log((7).toExponential(), (-0).toExponential(1));
console.log(Infinity.toExponential(101));
try { console.log((1).toExponential(101)); } catch (error) { console.log(error.name); }
try { Number.prototype.toExponential.call("1", 2); } catch (error) { console.log(error.name); }Result
1.23e+3 1.25e-3 7e+0 0.0e+0 Infinity RangeError TypeError
The example shows fixed digits, omitted digits, negative zero, and the Infinity-versus-RangeError order.