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 len is not passed as a parameter, it is set to the number of digits necessary to represent the number.
  • len must 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.199
console.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, so len is set to len = 4, as num has 5 digits and the first digit is placed before the decimal point. This is why we need 4 decimal places.
  • line 5: len = 0 is passed to the toExponential() method, so there is no decimal place, and num is rounded to a whole number.
  • line 7: len = 7 is passed to the toExponential() method, so 3 zeros are added to make 7 decimal places.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved