Rule: no-magic-numbers

Disallows the use constant number values outside of variable assignments. When no list of allowed values is specified, -1, 0 and 1 are allowed by default.

Rationale

Magic numbers should be avoided as they often lack documentation. Forcing them to be stored in variables gives them implicit documentation.

Config

Options may either be a list of numbers to ignore (not consider ‘magic’), or an object containing up to two properties:

  • allowed-numbers as the list of numbers to ignore.
  • ignore-jsx to specify that ‘magic’ numbers should be allowed as JSX attributes.
Config examples
"no-magic-numbers": [true, 1, 2, 3]
"no-magic-numbers": [true, {"allowed-numbers": [1, 2, 3], "ignore-jsx": true}]
Schema
{
  "type": "array",
  "items": {
    "type": "number"
  },
  "properties": {
    "type": "object",
    "allowed-numbers": {
      "type": "array"
    },
    "ignore-jsx": {
      "type": "boolean"
    }
  },
  "minLength": 1
}