‘Magic numbers’ are numbers that occur multiple time in code without an explicit meaning. They should preferably be replaced by named constants.
var now = Date.now(), inOneHour = now + (60 * 60 * 1000);
The no-magic-numbers
rule aims to make code more readable and refactoring easier by ensuring that special numbers are declared as constants to make their meaning explicit.
Examples of incorrect code for this rule:
/*eslint no-magic-numbers: "error"*/ var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * 0.25);
/*eslint no-magic-numbers: "error"*/ var data = ['foo', 'bar', 'baz']; var dataLast = data[2];
/*eslint no-magic-numbers: "error"*/ var SECONDS; SECONDS = 60;
Examples of correct code for this rule:
/*eslint no-magic-numbers: "error"*/ var TAX = 0.25; var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * TAX);
An array of numbers to ignore. It’s set to []
by default. If provided, it must be an Array
.
Examples of correct code for the sample { "ignore": [1] }
option:
/*eslint no-magic-numbers: ["error", { "ignore": [1] }]*/ var data = ['foo', 'bar', 'baz']; var dataLast = data.length && data[data.length - 1];
A boolean to specify if numbers used as array indexes are considered okay. false
by default.
Examples of correct code for the { "ignoreArrayIndexes": true }
option:
/*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/ var data = ['foo', 'bar', 'baz']; var dataLast = data[2];
A boolean to specify if we should check for the const keyword in variable declaration of numbers. false
by default.
Examples of incorrect code for the { "enforceConst": true }
option:
/*eslint no-magic-numbers: ["error", { "enforceConst": true }]*/ var TAX = 0.25; var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * TAX);
A boolean to specify if we should detect numbers when setting object properties for example. false
by default.
Examples of incorrect code for the { "detectObjects": true }
option:
/*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/ var magic = { tax: 0.25 }; var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax);
Examples of correct code for the { "detectObjects": true }
option:
/*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/ var TAX = 0.25; var magic = { tax: TAX }; var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax);
This rule was introduced in ESLint 1.7.0.
© JS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/rules/no-magic-numbers