Operator syntax
Until the ES2016 standard, there was no exponential operator in JavaScript, but the method Math.pow was used. Now the “**” operator returns the result of scaling the base (first operand) with the exponent (second operand):
<strong>x<sup>y</sup></strong> = Math.pow(x, y) = <strong>x**y</strong>
Example
|
1 2 |
x ** y let kub = 2 ** 3; // Zamena za: 2 * 2 * 2 |
Example
In addition to this standard operator, we can use it with the operator “=”:
|
1 2 |
let b = 2; b **= 3; // Isto kao: b = b * b * b; |
NOTE:
In JavaScript, it is not allowed to write so-called ambiguous code, so the following example returns an error:
|
1 |
-2 ** 2; // U javaScriptu je invalid operacija, 4 u Bash, -4 u drugim jezicima. |
It is therefore necessary to make it clear which operation has priority:
|
1 |
-(2 ** 2); // -4 u JavaScript-u |
or
|
1 |
(-2) ** 2 // 4 |
