]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/radix.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / radix.md
1 # Require Radix Parameter (radix)
2
3 When using the `parseInt()` function it is common to omit the second argument, the radix, and let the function try to determine from the first argument what type of number it is. By default, `parseInt()` will autodetect decimal and hexadecimal (via `0x` prefix). Prior to ECMAScript 5, `parseInt()` also autodetected octal literals, which caused problems because many developers assumed a leading `0` would be ignored.
4
5 This confusion led to the suggestion that you always use the radix parameter to `parseInt()` to eliminate unintended consequences. So instead of doing this:
6
7 ```js
8 var num = parseInt("071"); // 57
9 ```
10
11 Do this:
12
13 ```js
14 var num = parseInt("071", 10); // 71
15 ```
16
17 ECMAScript 5 changed the behavior of `parseInt()` so that it no longer autodetects octal literals and instead treats them as decimal literals. However, the differences between hexadecimal and decimal interpretation of the first parameter causes many developers to continue using the radix parameter to ensure the string is interpreted in the intended way.
18
19 On the other hand, if the code is targeting only ES5-compliant environments passing the radix `10` may be redundant. In such a case you might want to disallow using such a radix.
20
21 ## Rule Details
22
23 This rule is aimed at preventing the unintended conversion of a string to a number of a different base than intended or at preventing the redundant `10` radix if targeting modern environments only.
24
25 ## Options
26
27 There are two options for this rule:
28
29 * `"always"` enforces providing a radix (default)
30 * `"as-needed"` disallows providing the `10` radix
31
32 ### always
33
34 Examples of **incorrect** code for the default `"always"` option:
35
36 ```js
37 /*eslint radix: "error"*/
38
39 var num = parseInt("071");
40
41 var num = parseInt(someValue);
42
43 var num = parseInt("071", "abc");
44
45 var num = parseInt("071", 37);
46
47 var num = parseInt();
48 ```
49
50 Examples of **correct** code for the default `"always"` option:
51
52 ```js
53 /*eslint radix: "error"*/
54
55 var num = parseInt("071", 10);
56
57 var num = parseInt("071", 8);
58
59 var num = parseFloat(someValue);
60 ```
61
62 ### as-needed
63
64 Examples of **incorrect** code for the `"as-needed"` option:
65
66 ```js
67 /*eslint radix: ["error", "as-needed"]*/
68
69 var num = parseInt("071", 10);
70
71 var num = parseInt("071", "abc");
72
73 var num = parseInt();
74 ```
75
76 Examples of **correct** code for the `"as-needed"` option:
77
78 ```js
79 /*eslint radix: ["error", "as-needed"]*/
80
81 var num = parseInt("071");
82
83 var num = parseInt("071", 8);
84
85 var num = parseFloat(someValue);
86 ```
87
88 ## When Not To Use It
89
90 If you don't want to enforce either presence or omission of the `10` radix value you can turn this rule off.
91
92 ## Further Reading
93
94 * [parseInt and radix](https://davidwalsh.name/parseint-radix)