ES7 Exponentiation Operator

This tutorial covers exponential operator introduced in ES7, ES2016 javascript..

Introduction

exponentiation operator is one of minor feature introduced in EcmaScript2017(ES7) language.

It applies two asterick symbols ** on two operands which returns the result of first operand to power of second operand.

Important points

  • These is replacement of Math.pow(m,n) function in Math library
  • used in arthmatic,scientifical and 3D calculations
  • Both operands are variables or constants
  • right associative ie right to left, applied when multiple exponentional operators are used
  • Shorter syntax for power of values.

Syntax of exponetiation operator

Operand1 ** Operand2 which is equal to Operand1 power of Operand2

Operand1 is called as Base Operand2 is called as exponent For example, 5***3 is equal to 5 power of 3 is equal to 5*5*5 (125)

var m = 3;
var n = 2;
m ** n; // 9
Math.pow(m,n) // 9 returned


**Grouping operators from right to left**

operator is right associative which means applies from right to left
```javascript
m ** n ** 0 is equal to m ** (n ** o)

3 ** 2 ** 2 equl to 3**(2**2) // returns 91

Some of the valid and invalid examples are seen below

Binary operator, Unary opeartor is not allowed

Unary operators like +,-,delete and void are not allowed for base

Invalid usage

+2 **4
-2** 5

The above code results compilation error unknown: Illegal expression. Wrap left hand side or entire exponentiation in parentheses.

exponent NAN,null,undefined values

  • applying any operator on NAN as exponent results 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
Prev