JavaScript Number toExponential() Method - GeeksforGeeks

22 Dec, 2021
Below is the example of the Number toExponential() Method.
Example:        <script type="text/javascript">    var num = 212.13456;    document.write(num.toExponential(4));          </script>
        <script type="text/javascript">    var num = 212.13456;    document.write(num.toExponential(4));          </script>
Output:Output :2.1213e+2
Output :2.1213e+2
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() method is used with a number as shown in the above syntax using the ‘.’ operator. This method will convert a number to its exponential form.
Parameters: This method 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.
The below examples illustrates the working of the toExponential() method in JavaScript:
Passing a number as an argument in the toExponential() method. If a number is passed as an argument to the toExponential() method then it represents the number of digits after the decimal point.Code# 1:        <script type="text/javascript">    var num = 2.13456;    document.write(num.toExponential(2));          </script>Output:2.13e+0Passing no parameter in the toExponential() method. Below program illustrates this:Code# 2:        <script type="text/javascript">    var num = 2.13456;    document.write(num.toExponential());          </script>Output:2.13456e+0Passing a value which has more than 1 digit before the decimal point in the toExponential() method. Below program illustrates this:Code# 3:        <script type="text/javascript">    var num=212.13456;    document.write(num.toExponential());          </script>Output:2.1213456e+2Passing zero as a parameter in the toExponential() method. Below program illustrates this:Code# 4:        <script type="text/javascript">    var num = 212.13456;    document.write(num.toExponential(0));          </script>Output:Output :2e+2
Passing a number as an argument in the toExponential() method. If a number is passed as an argument to the toExponential() method then it represents the number of digits after the decimal point.Code# 1:        <script type="text/javascript">    var num = 2.13456;    document.write(num.toExponential(2));          </script>Output:2.13e+0
        <script type="text/javascript">    var num = 2.13456;    document.write(num.toExponential(2));          </script>
Output:
2.13e+0
Passing no parameter in the toExponential() method. Below program illustrates this:Code# 2:        <script type="text/javascript">    var num = 2.13456;    document.write(num.toExponential());          </script>Output:2.13456e+0
        <script type="text/javascript">    var num = 2.13456;    document.write(num.toExponential());          </script>
Output:
2.13456e+0
Passing a value which has more than 1 digit before the decimal point in the toExponential() method. Below program illustrates this:Code# 3:        <script type="text/javascript">    var num=212.13456;    document.write(num.toExponential());          </script>Output:2.1213456e+2
        <script type="text/javascript">    var num=212.13456;    document.write(num.toExponential());          </script>
Output:
2.1213456e+2
Passing zero as a parameter in the toExponential() method. Below program illustrates this:Code# 4:        <script type="text/javascript">    var num = 212.13456;    document.write(num.toExponential(0));          </script>Output:Output :2e+2
        <script type="text/javascript">    var num = 212.13456;    document.write(num.toExponential(0));          </script>
Output:
Output :2e+2
Exceptions:
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() method.Type Error: This exception is thrown when the toFixed() method is invoked on an object that is not of type number.
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() method.
Type Error: This exception is thrown when the toFixed() method is invoked on an object that is not of type number.
Supported Browsers:
Google Chrome 1 and above
Internet Explorer 5.5 and above
Firefox 1 and above
Apple Safari 2 and above
Opera 7 and above
ysachin2314
JavaScript-Methods
JavaScript-Numbers
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?