]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: prefer-exponentiation-operator | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | further_reading: | |
5 | - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation | |
6 | - https://bugs.chromium.org/p/v8/issues/detail?id=5848 | |
7 | --- | |
8 | ||
9 | ||
eb39fafa DC |
10 | |
11 | Introduced in ES2016, the infix exponentiation operator `**` is an alternative for the standard `Math.pow` function. | |
12 | ||
13 | Infix notation is considered to be more readable and thus more preferable than the function notation. | |
14 | ||
15 | ## Rule Details | |
16 | ||
17 | This rule disallows calls to `Math.pow` and suggests using the `**` operator instead. | |
18 | ||
19 | Examples of **incorrect** code for this rule: | |
20 | ||
8f9d1d4d DC |
21 | ::: incorrect |
22 | ||
eb39fafa DC |
23 | ```js |
24 | /*eslint prefer-exponentiation-operator: "error"*/ | |
25 | ||
26 | const foo = Math.pow(2, 8); | |
27 | ||
28 | const bar = Math.pow(a, b); | |
29 | ||
30 | let baz = Math.pow(a + b, c + d); | |
31 | ||
32 | let quux = Math.pow(-1, n); | |
33 | ``` | |
34 | ||
8f9d1d4d DC |
35 | ::: |
36 | ||
eb39fafa DC |
37 | Examples of **correct** code for this rule: |
38 | ||
8f9d1d4d DC |
39 | ::: correct |
40 | ||
eb39fafa DC |
41 | ```js |
42 | /*eslint prefer-exponentiation-operator: "error"*/ | |
43 | ||
44 | const foo = 2 ** 8; | |
45 | ||
46 | const bar = a ** b; | |
47 | ||
48 | let baz = (a + b) ** (c + d); | |
49 | ||
50 | let quux = (-1) ** n; | |
51 | ``` | |
52 | ||
8f9d1d4d DC |
53 | ::: |
54 | ||
eb39fafa DC |
55 | ## When Not To Use It |
56 | ||
57 | This rule should not be used unless ES2016 is supported in your codebase. |