Exponential Operator In JavaScript

Exponential Operator In JavaScript | One kind of mathematical operator is the exponential operator. The first operand is raised to the power of the second operand using JavaScript exponential. Two stars, or **, are the standard symbol for this operator. Scripting languages like Python and PHP also adhere to the same syntax.

The JavaScript exponential method’s behavior is identical to that of the Math.pow() method. Finding solutions to mathematical formulas that resemble x to the power y is the most fundamental application. By increasing a negative number to the power of a positive number, it may also perform further operations like discovering square roots, cube roots, and so forth (x to the power negative y).

The fact that JavaScript can accept BigInts as operands is its primary benefit over the Math.pow() function. Additionally, it can take any operand to be a floating point number or a number with decimal points.

The ** sign is used between two numbers to return the left operand to the power of the right operand, which is a very straightforward operation.

The following code displays the result of 5 raised to power 2, which is equal to 25.

num = 5 ** 2;
console.log(num);

Output:-

25

The next piece of code displays the square root of 49, that is, 7 raised to -2.

num = 49 ** (0.5);
console.log(num);

Output:-

7

The toExponential() method in JavaScript is demonstrated by the examples below:-

1. Using the toExponential() function with an integer as an argument. The number of digits after the decimal point is represented by a number if it is supplied as an argument to the toExponential() method.

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

Output:-

4.8348e+0

2. Using the toExponential() function with no parameters. The code below demonstrates this.

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

Output:-

4.8348e+0

3. 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

4. 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

Special cases:-

Type Error:- This error is triggered when the toFixed() method is called on an object that isn’t of the type number.

Range Error:- This exception is raised when a value argument is specified that is either too small or too large. There is no RangeError for the range 0 to 20, inclusive. You must properly implement the toExponential() method if you want to pass values that are bigger or lower than those permitted by this range.

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. Required fields are marked *