JavaScript toExponential() Method

JavaScript toExponential() Method | The toExponential() method in JavaScript returns a string containing a number represented in exponential notation.

JavaScript toExponential() Method Syntax:-
toExponential(fractionDigits?: number): string;

It returns a string containing a number represented in exponential notation.
Parameter, fractionDigits:- Number of digits after the decimal point. Must be in the range 0 – 20, inclusive.

JavaScript toExponential() Method Example

let us see some examples of the JavaScript toExponential() method.

var num = 4.8348;
console.log(num.toExponential(4))

Output:-

4.8348e+0

JavaScript toExponential() Method Example With No Parameters

The code below demonstrates the example of the toExponential() function with no parameters:-

var num = 4.8348;
console.log(num.toExponential())

Output:-

4.8348e+0

Using the toExponential() method with a value that has more than one digit before the decimal point. The code below demonstrates this:-

var num = 4843.8348;
console.log(num.toExponential())

Output:-

4.8438348e+3

Using the toExponential() function with the argument zero. The program below demonstrates this:-

var num = 4843.8348;
console.log(num.toExponential(0))

Output:-

5e+3

var num = 4843.8348;
console.log(num.toExponential('Hello'))

Output:-

5e+3

var num = 4843.8348;
console.log(num.toExponential(NaN))

Output:-

5e+3

var num = 1234.56789;
console.log(num.toExponential(NaN))

Output:-

1e+3

Error given by JavaScript toExponential() Method

Range Error: When a value parameter is supplied that is either too little or too large, this exception is raised. The range 0 to 100 inclusively does not result in a RangeError. If you want to pass numbers that are greater or smaller than those allowed by this range, you must implement the toExponential() method appropriately.

var num = 4843.8348;
console.log(num.toExponential(-5))

Error:-

console.log(num.toExponential(-5))
^
RangeError: toExponential() argument must be between 0 and 100

var num = 4843.8348;
console.log(num.toExponential(101))

Error:-

console.log(num.toExponential(101))
^
RangeError: toExponential() argument must be between 0 and 100

TypeError: When the toExponential() method is called on an object that isn’t of the type number, then TypeError is raised.

var num = "1234.56789";
console.log(num.toExponential(2))

Error:-

console.log(num.toExponential(2))
^
TypeError: num.toExponential is not a function

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.