Modern JavaScript Web Development Cookbook
上QQ阅读APP看书,第一时间看更新

Doing powers

Finally, let's introduce a newly added operator, **, which stands for power calculations:

let a = 2 ** 3; // 8

This is just a shortcut for the existing Math.pow() function:

let b = Math.pow(2, 3); // also 8

An exponential assignment operator also exists, which is similar to +=, -=, and the rest:

let c = 4;
c **= 3; // 4 cubed: 64

This is an operator that you won't probably using very often, unless you deal with interest calculations and financial formulas. A final reminder: just as in math, the exponentiation operator groups from right to left, so 2 ** 3 ** 4 is calculated as 2 ** (3 ** 4); be careful!