What is toExponential() in Javascript?
toExponential() is a Number method that formats a string into an exponential notation. The toExponential() method is declared as follows:
num.toExponential(len)
num: The number to be converted to an exponential notation.len: The number of digits after the decimal point.
Note:
- If
lenis not passed as a parameter, it is set to the number of digits necessary to represent the number.lenmust be in the range 1 to 100. Otherwise, RangeError is thrown.
Return value
The toExponential() method returns a string that represents the number num in exponential notation.
Browser compatibility
The toExponential() method is supported by the following browsers:
- Edge
- Firefox
- Google Chrome
- Opera
- Safari
Example
Consider the code snippet below, which demonstrates the use of the toExponential() method:
var num = 15.199console.log("15.199 to precision of no length is: ",num.toExponential());console.log("15.199 to precision of length 3 is: ",num.toExponential(0));console.log("15.199 to precision of length 7 is: ",num.toExponential(7));
Explanation
A number num is initialized with num = 15.199 in line 1.
- line 3: The
toExponential()method is not passed the input parameter, solenis set tolen = 4, asnumhas 5 digits and the first digit is placed before the decimal point. This is why we need 4 decimal places. - line 5:
len = 0is passed to thetoExponential()method, so there is no decimal place, andnumis rounded to a whole number. - line 7:
len = 7is passed to thetoExponential()method, so 3 zeros are added to make 7 decimal places.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved