1 # Disallow Magic Numbers (no-magic-numbers)
3 'Magic numbers' are numbers that occur multiple times in code without an explicit meaning.
4 They should preferably be replaced by named constants.
8 inOneHour = now + (60 * 60 * 1000);
13 The `no-magic-numbers` rule aims to make code more readable and refactoring easier by ensuring that special numbers
14 are declared as constants to make their meaning explicit.
16 Examples of **incorrect** code for this rule:
19 /*eslint no-magic-numbers: "error"*/
21 var dutyFreePrice = 100,
22 finalPrice = dutyFreePrice + (dutyFreePrice * 0.25);
26 /*eslint no-magic-numbers: "error"*/
28 var data = ['foo', 'bar', 'baz'];
30 var dataLast = data[2];
34 /*eslint no-magic-numbers: "error"*/
41 Examples of **correct** code for this rule:
44 /*eslint no-magic-numbers: "error"*/
48 var dutyFreePrice = 100,
49 finalPrice = dutyFreePrice + (dutyFreePrice * TAX);
56 An array of numbers to ignore. It's set to `[]` by default.
57 If provided, it must be an `Array`.
59 The array can contain values of `number` and `string` types.
60 If it's a string, the text must be parsed as `bigint` literal (e.g., `"100n"`).
62 Examples of **correct** code for the sample `{ "ignore": [1] }` option:
65 /*eslint no-magic-numbers: ["error", { "ignore": [1] }]*/
67 var data = ['foo', 'bar', 'baz'];
68 var dataLast = data.length && data[data.length - 1];
71 Examples of **correct** code for the sample `{ "ignore": ["1n"] }` option:
74 /*eslint no-magic-numbers: ["error", { "ignore": ["1n"] }]*/
79 ### ignoreArrayIndexes
81 A boolean to specify if numbers used in the context of array indexes (e.g., `data[2]`) are considered okay. `false` by default.
83 This option allows only valid array indexes: numbers that will be coerced to one of `"0"`, `"1"`, `"2"` ... `"4294967294"`.
85 Arrays are objects, so they can have property names such as `"-1"` or `"2.5"`. However, those are just "normal" object properties that don't represent array elements. They don't influence the array's `length`, and they are ignored by array methods like `.map` or `.forEach`.
87 Additionally, since the maximum [array length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) is 2<sup>32</sup> - 1, all values above 2<sup>32</sup> - 2 also represent just normal property names and are thus not considered to be array indexes.
89 Examples of **correct** code for the `{ "ignoreArrayIndexes": true }` option:
92 /*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/
100 a = data[-0]; // same as data[0], -0 will be coerced to "0"
106 a = data[10n]; // same as data[10], 10n will be coerced to "10"
108 a = data[4294967294]; // max array index
111 Examples of **incorrect** code for the `{ "ignoreArrayIndexes": true }` option:
114 /*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/
116 f(2); // not used as array index
126 a = data[4294967295]; // above the max array index
128 a = data[1e500]; // same as data["Infinity"]
133 A boolean to specify if we should check for the const keyword in variable declaration of numbers. `false` by default.
135 Examples of **incorrect** code for the `{ "enforceConst": true }` option:
138 /*eslint no-magic-numbers: ["error", { "enforceConst": true }]*/
142 var dutyFreePrice = 100,
143 finalPrice = dutyFreePrice + (dutyFreePrice * TAX);
148 A boolean to specify if we should detect numbers when setting object properties for example. `false` by default.
150 Examples of **incorrect** code for the `{ "detectObjects": true }` option:
153 /*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/
159 var dutyFreePrice = 100,
160 finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax);
163 Examples of **correct** code for the `{ "detectObjects": true }` option:
166 /*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/
174 var dutyFreePrice = 100,
175 finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax);