JavaScript | toExponential() Function

The toExponential() method in JavaScript is used to convert a number to its exponential form. It returns a string representing the Number object in exponential notation.

Syntax:

number.toExponential(value)

The toExponential() function is used with a number as shown in above syntax using the ‘.’ operator. This function will convert a number to its exponential form.

Parameters: This function accepts a single parameter value . It is an optional parameter and it represents the value specifying the number of digits after the decimal point.

Return Value: The toExponential() method in JavaScript returns a string representing the given number in exponential notation with one digit before the decimal point.



Below examples illustrates the working of toExponential() function in JavaScript:

  1. Passing a number as an argument in the toExponential() method. If a number is passed as argument to the toExponential() function then it represents the number of digits after decimal point.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

            
    <script type="text/javascript">
        var num = 2.13456;
        document.write(num.toExponential(2));          
    </script>

    chevron_right

    
    

    Output:

    2.13e+0
  2. Passing no parameter in the toExponential() method. Below program illustrates this:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

            
    <script type="text/javascript">
        var num = 2.13456;
        document.write(num.toExponential());          
    </script>

    chevron_right

    
    

    Output:

    2.13456e+0
  3. Passing a value which has more than 1 digit before the decimal point in the toExponential() method. Below program illustrates this:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

            
    <script type="text/javascript">
        var num=212.13456;
        document.write(num.toExponential());          
    </script>

    chevron_right

    
    

    Output:

    2.1213456e+2
  4. Passing zero as a parameter in the toExponential() method. Below program illustrates this:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

            
    <script type="text/javascript">
        var num = 212.13456;
        document.write(num.toExponential(0));          
    </script>

    chevron_right

    
    

    Output:

    Output :2e+2

Exceptions:

  1. Range Error: This exception is thrown when the value parameter passed is too small or too large. Values between 0 and 20, inclusive, will not cause a RangeError. If you want to pass larger or smaller values than specified by this range then you have to accordingly implement the toExponential() function.
  2. Type Error: This exception is thrown when the toFixed() method is invoked on an object that is not of type number.


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.