]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/prefer-exponentiation-operator.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / prefer-exponentiation-operator.md
CommitLineData
8f9d1d4d
DC
1---
2title: prefer-exponentiation-operator
8f9d1d4d
DC
3rule_type: suggestion
4further_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
11Introduced in ES2016, the infix exponentiation operator `**` is an alternative for the standard `Math.pow` function.
12
13Infix notation is considered to be more readable and thus more preferable than the function notation.
14
15## Rule Details
16
17This rule disallows calls to `Math.pow` and suggests using the `**` operator instead.
18
19Examples of **incorrect** code for this rule:
20
8f9d1d4d
DC
21::: incorrect
22
eb39fafa
DC
23```js
24/*eslint prefer-exponentiation-operator: "error"*/
25
26const foo = Math.pow(2, 8);
27
28const bar = Math.pow(a, b);
29
30let baz = Math.pow(a + b, c + d);
31
32let quux = Math.pow(-1, n);
33```
34
8f9d1d4d
DC
35:::
36
eb39fafa
DC
37Examples of **correct** code for this rule:
38
8f9d1d4d
DC
39::: correct
40
eb39fafa
DC
41```js
42/*eslint prefer-exponentiation-operator: "error"*/
43
44const foo = 2 ** 8;
45
46const bar = a ** b;
47
48let baz = (a + b) ** (c + d);
49
50let quux = (-1) ** n;
51```
52
8f9d1d4d
DC
53:::
54
eb39fafa
DC
55## When Not To Use It
56
57This rule should not be used unless ES2016 is supported in your codebase.