exponentiation operator is one of the minor features introduced in EcmaScript2017(ES7) language.
It uses two asterisk symbols ‘**’ on two operands to return the result with the first operand multiplied by the power of the second operand. Important points
- This is a replacement of the
Math.pow(m,n)function in the Math library
- used in arithmetic, scientifically, and 3D calculations
- Both operands are variables or constants
- right-associative i.e right to left, applied when multiple exponential operators are used.
- Shorter syntax for the power of values.
Syntax of exponentiation operator
Operand1 ** Operand2 that is equal to Operand1 power of Operand2
Operand1 is called a
Operand2 is called an
5***3 is equal to
5 power of 3 is equal to
var m = 3; var n = 2; m ** n; // 9 Math.pow(m,n) // 9 returned
Grouping operators from right to left
the operator is right-associative which means applies from right to left
m ** n ** 0 is equal to m ** (n ** o) 3 ** 2 ** 2 equl to 3**(2**2) // returns 91
Let’s see some of the valid and invalid examples.
Binary operator, Unary operator is not allowed
Unary operators like +,-, delete, and void is not allowed for the base.
+2 **4 -2** 5
The above code results compilation error unknown: Illegal expression. Wrap the left-hand side or entire exponentiation in parentheses.
exponent operator on NAN,null,undefined values
- applying any operator on NAN as exponent result always NAN.
- null will treat as zero and apply null results to 1
- undefined exponent results NAN
2 ** NAN //NAN 2 ** undefined //NAN 2 ** null // 1
Exponentiation Operator Assignment(**=) example
Assignments are shorthand operators for Exponentiation operations.
For example ``markup p**=q is equal to p=p** q
Like a normal operator, if used with string, it returns the