EN
ES7 / ES2016 / ECMAScript 2016 - Exponentiation operator (**) example
3 points
In this article, we would like to show you how to use Exponentiation Operator (**
) that has been introduced in ECMAScript 2016.
xxxxxxxxxx
1
console.log( 1 ** 2 ); // 1
2
console.log( 2 ** 2 ); // 4
3
console.log( 3 ** 2 ); // 9
4
5
console.log( 0 ** 3 ); // 0
6
console.log( 0.5 ** 0.3 ); // 0.8122523963562356
7
console.log( (-1) ** 4 ); // 1
8
9
console.log( 0 ** -0.4 ); // Infinity
10
console.log( (-0.5) ** -2 ); // 4
11
console.log( (-2.0) ** -2 ); // 0.25
12
console.log( (-2.0) ** 0.5 ); // NaN
Note:
For negative literal base values we need to use brackets becase of the syntax error, e.g.
(-2.0) ** 0.5
- correct syntax-2.0 ** 0.5
- incorrect syntax