Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The enforceConst option of the no-magic-numbers rule incorrectly reports a simple for loop

See original GitHub issue

Tell us about your environment

  • ESLint Version: v4.19.1
  • Node Version: v8.9.4
  • npm Version: 6.0.0

What parser (default, Babel-ESLint, etc.) are you using? default

Please show your full configuration:

    "env": {
        "browser": true,
        "es6": true,
        "greasemonkey": true
    "extends": "eslint:recommended",
    "globals": {
        "min": false
    "rules": {
        "array-callback-return": "error",
        "block-scoped-var": "error",
        "curly": "error",
        "dot-notation": "error",
        "eol-last": [
        "eqeqeq": "error",
        "guard-for-in": "error",
        "indent": [
            4, {
                "SwitchCase": 1
        "linebreak-style": [
        "no-case-declarations": "error",
        "no-else-return": "error",
        "no-empty-function": "error",
        "no-eq-null": "error",
        "no-eval": "error",
        "no-extra-bind": "error",
        "no-floating-decimal": "error",
        "no-implicit-coercion": "error",
        "no-implicit-globals": "error",
        "no-implied-eval": "error",
        "no-invalid-this": "error",
        "no-loop-func": "error",
        "no-lone-blocks": "error",
        "no-magic-numbers": [
            "error", {
                "enforceConst": true,
                "ignoreArrayIndexes": true
        "no-multiple-empty-lines": [
            "error", {
                "max": 1,
                "maxBOF": 0,
                "maxEOF": 1
        "no-trailing-spaces": "error",
        "quotes": [
        "semi": [
        "valid-jsdoc": [
            "error", {
                "requireReturn": false

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

(function () {
    var array = [];

    for (let i = 0; i < array.length; i++) {
        array[i] = i;
./node_modules/.bin/eslint index.js

What did you expect to happen?

Not throw an error.

What actually happened? Please include the actual, raw output from ESLint.

  4:18  error  Number constants declarations must use 'const'  no-magic-numbers

✖ 1 problem (1 error, 0 warnings)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

not-an-aardvarkcommented, May 23, 2018

I think we’re straying from the point of the enforceConst option. Imagine the code looked like this:

for (let i = 4124; i < arr.length; i++) {
    // ...

I would argue that the use of 4124 is a code smell, because it’s a “magic number” and provides no readable info about what that particular value is for.

The correct fix for the issue would be to do something like this:

const INITIAL_NUM_WIDGETS = 4124; // (e.g.)

for (let i = INITIAL_NUM_WIDGETS; i < arr.length; i++) {
    // ...

The enforceConst option on the no-magic-numbers rule controls whether the declaration of INITIAL_NUM_WIDGETS must be declared with const, or whether var is also acceptable. It’s not related to the initialization of i.

If the purpose of the rule is to give a name to special constants, then I don’t think it makes sense to accept a loop iteration variable as the “name” of the constant, because loop iteration variables change their value, so they would not consistently refer to that constant.

For example, the following correction to the original code would not make sense:

// rename `i` to `INITIAL_NUM_WIDGETS`
    // ...

I would argue that the above code still has the same code smell as the original code, because it’s still not clear where the value of 4124 comes from. The name INITIAL_NUM_WIDGETS doesn’t clarify the situation, because it refers to a value that changes within the loop, rather than always referring to the value 4124.

revoltercommented, Dec 20, 2018

@not-an-aardvark, Does my last comment make sense?

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-magic-numbers - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
no-magic-numbers - TypeScript ESLint
This rule extends the base eslint/no-magic-numbers rule. It adds support for: ... Note: you must disable the base rule as it can report...
Read more >
Disallow magic numbers ( no-magic-numbers ) - GitLab
This rule extends the base eslint/no-magic-numbers rule. ... the base rule as it can report incorrect errors "no-magic-numbers": "off", ...
Read more >
What is a magic number, and why is it bad? - Stack Overflow
First, magic numbers are not just numbers. Any basic value can be "magic". Basic values are manifest entities such as integers, reals, doubles,...
Read more >
no-magic-numbers - Rules - ESLint中文文档
The no-magic-numbers rule aims to make code more readable and refactoring easier by ... Examples of incorrect code for the { "enforceConst": true...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found