totn JavaScript

JavaScript: Number toExponential() method

This JavaScript tutorial explains how to use the Number method called toExponential() with syntax and examples.

Description

In JavaScript, toExponential() is a Number method that is used to convert a number to exponential notation and return its value as a string. Because toExponential() is a method of the Number object, it must be invoked through a particular instance of the Number class.

Syntax

In JavaScript, the syntax for the toExponential() method is:

number.toExponential([fractionalDigits]);

Parameters or Arguments

fractionalDigits
Optional. It is the number of fractional digits to display in the result. If this parameter is omitted, up to 16 fractional digits may appear in the result, depending on your browser.

Returns

The toExponential() method converts a number to exponential notation and then returns its value as a string.

Note

  • The toExponential() method does not change the value of the original number.

Example

Let's take a look at an example of how to use the toExponential() method in JavaScript.

For example:

var totn_number = 123.456789;

console.log(totn_number.toExponential());
console.log(totn_number.toExponential(1));
console.log(totn_number.toExponential(2));

In this example, we have declared a variable called totn_number that is assigned the value of 123.456789. We have then invoked the toExponential() method of the totn_number to convert the number to exponential notation represented as a string.

We have written the output of the toExponential() method to the web browser console log, for demonstration purposes, to show what the toExponential() method returns.

The following will be output to the web browser console log:

1.23456789e+2
1.2e+2
1.23e+2

In this example, the first output to the console log returned the string value "1.23456789e+2" which is the exponential notation for the number 123.456789.

The second output to the console log returned the string value "1.2e+2" which is the exponential notation for the number 123.456789 with 1 fractional digit.

The third output to the console log returned the string value "1.23e+2" which is the exponential notation for the number 123.456789 with 2 fractional digits.