# What is Number.toExponential() method in JavaScript?

In JavaScript, the Number.toExponential() method returns a string of a number representing the given number in its exponential form. This method is basically used to convert a number to its exponential form.

## Syntax

The following syntax will show you how you can use the Number.toExponential() method to convert a number into its exponential value

Number.toExponential( fractionDigits );


The fractionDigits parameter is an integer specifying the number of digits after the decimal point.

Let us understand the implementation of the toExponential() method in details with the help of code examples

## Algorithm

Step 1 − In the first step of the algorithm, we will add a input bar in the code to get the floating point number input from the user and then operate the toExponential() method on that.

Step 2 − In this step, we will add a button element to the HTML document with onclick event associated to it, which later calls a JavaScript function to show results to the user.

Step 3 − In the next step, we will define a JavaScript function and then pass it to the onclick event of the button as its value, so that it can be called later on button click.

Step 4 − In the last step, we will grab the value entered by the user and then operate it with the toExponential() method to return its exponential value.

## Example

You can try to run the following code to learn how to work with Number.toExponential() method in JavaScript

<html>
<head>
<title>JavaScript Method toExponential()</title>
</head>
<body>
<script>
var num=77.1234;
var val = num.toExponential();
document.write("num.toExponential() is : " + val );
document.write("<br />");
val = num.toExponential(4);
document.write("num.toExponential(4) is : " + val );
document.write("<br />");
val = num.toExponential(2);
document.write("num.toExponential(2) is : " + val);
document.write("<br />");
val = 77.1234.toExponential();
document.write("77.1234.toExponential()is : " + val );
document.write("<br />");
val = 77.1234.toExponential();
document.write("77 .toExponential() is : " + val);
</script>
</body>
</html>


## Example

The below example will illustrates the practical implementation of the toExponential() method

<!DOCTYPE html>
<html>
<body>
<h2> Working with Number.toExponential() method in JavaScript </h2>
<p> Click the below button to see the results. </p>
<h4> Enter a floating point number: </h4>
<input type = "number" id = "inp1"><br> <br>
<button onclick = "display()"> Check here </button>
<p id = "result"> </p>
<script>
var result = document.getElementById("result");
function display() {
var inp1 = document.getElementById("inp1");
var num = Number(inp1.value);
var res = num.toExponential(4);
result.innerHTML += num +".toExponential(4) = " + res + " <br> ";
}
</script>
</body>
</html>


In the above example, we have grab the value entered by the user as num and then apply the toExponential() method to it and store that value in a new variable name res. So, the final exponential value of the given number is stored in the res variable. We have passed 4 as parameter to the toExponential() method which represents 4 digits will be shown to the user after the decimal point. Let us see another example where we will not pass any number as the parameter to the toExpoenetial() method.

Algorithm − The algorithm of the above example and this example is almost same you just need to use the toExponential() method without any parameter such that with empty paranthesis unlike of above example where we have passed 4 as parameter inside the toExponential() method.

## Example

The below example will explain the behavior and the use of the toExponential() method without any parameter passed to it −

<!DOCTYPE html>
<html>
<body>
<h2> Working witth Number.toExponential() method in JavaScript </h2>
<p> Click the below button to see the results. </p>
<h4> Enter a floating point number: </h4&g>
<input type = "number" id = "inp1"> <br> <br>
<button onclick = "display()"> Check here </button>
<p id = "result"> </p>
<script>
var result = document.getElementById("result");
function display() {
var inp1 = document.getElementById("inp1");
var num = Number(inp1.value);
var res = num.toExponential();
result.innerHTML += num + ".toExponential() = " + res + " <br> ";
}
</script>
</body>
</html>

In the above example, we have used the toExponential() method without any parameter passed inside the paranthesis of it. As a result of which we will get the final resultant exponential value with many number of digits after the decimal point.

In this article, we have learned about the Number.toExponential() method in JavaScript by understanding the detailed use of this method to convert a number to its corresponding exponential value with the help of two different code examples. The code examples we have discussed are different as they show the behaviour of the toExponential() method in two different scenarios, where the former one show the use of the method with a parameter passed to it and another shows without any parameter passed inside the paranthesis of the method

Updated on: 06-Jan-2023

87 Views

##### Kickstart Your Career

Get certified by completing the course

Advertisements