]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/prefer-exponentiation-operator.md
first commit
[pve-eslint.git] / eslint / docs / rules / prefer-exponentiation-operator.md
1 # Disallow the use of `Math.pow` in favor of the `**` operator (prefer-exponentiation-operator)
2
3 Introduced in ES2016, the infix exponentiation operator `**` is an alternative for the standard `Math.pow` function.
4
5 Infix notation is considered to be more readable and thus more preferable than the function notation.
6
7 ## Rule Details
8
9 This rule disallows calls to `Math.pow` and suggests using the `**` operator instead.
10
11 Examples of **incorrect** code for this rule:
12
13 ```js
14 /*eslint prefer-exponentiation-operator: "error"*/
15
16 const foo = Math.pow(2, 8);
17
18 const bar = Math.pow(a, b);
19
20 let baz = Math.pow(a + b, c + d);
21
22 let quux = Math.pow(-1, n);
23 ```
24
25 Examples of **correct** code for this rule:
26
27 ```js
28 /*eslint prefer-exponentiation-operator: "error"*/
29
30 const foo = 2 ** 8;
31
32 const bar = a ** b;
33
34 let baz = (a + b) ** (c + d);
35
36 let quux = (-1) ** n;
37 ```
38
39 ## When Not To Use It
40
41 This rule should not be used unless ES2016 is supported in your codebase.
42
43 ## Further Reading
44
45 * [MDN Arithmetic Operators - Exponentiation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Exponentiation)
46 * [Issue 5848: Exponentiation operator ** has different results for numbers and variables from 50 upwards](https://bugs.chromium.org/p/v8/issues/detail?id=5848)