Exponential In JavaScript

Exponential In JavaScript | The exponential operator is a type of mathematical operator. JavaScript exponential is used to raise the first operand to the power of the second one. The symbol used for this operator is two stars (**). The same syntax is followed by languages like Python and PHP as well. Also see:- JavaScript toExponential() Method

The Math.pow() method provides the same functionality as that of JavaScript exponential. The most basic usage is finding answers to mathematical expressions which look like x to the power y. Apart from that, it can have other capabilities like finding square roots, cube roots, and so on, by raising a negative number to the power of a number (x to the power negative y).

Exponential in JavaScript Example

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 1/2 or 0.5.

num = 49 ** (1/2);
console.log(num);

Output:-

7

Any number raised to power 0 always produces a result of 1. The below program demonstrate this example.

num = 81 ** 0;
console.log(num);

Output:-

1

num = 81 ** -2;
console.log(num);

Output:-

0.00015241579027587258

Exponential in JavaScript Example with Floating point value

num = 81 ** 1.5;
console.log(num);

Output:-

729

num = 81 ** '0.5';
console.log(num);

num1 = 81 ** 'Hello';
console.log(num1);

Output:-

9
NaN

If any of the operands of the exponential operator is not number type then the exponential operator always produces NaN as result.

Exponential in JavaScript Example with Non-String Value

num = 'Hello' ** 2;
console.log(num);

num1 = 'Hello' ** 'World';
console.log(num1);

Output:-

NaN
NaN

Exponent Operator in JS vs Math.pow()

The main advantage of JavaScript exponential over the Math.pow() method is that it can accept BigInts as operands. It can also accept floating point numbers, that is, a number with decimal points as any of the operands. 

The usage is fairly simple, you just need to use the ** symbol between two numbers and it will return the left operand to the power of the right one. 

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 *