Search code examples
ecmascript-6rubyminerubymine-7

const or let which use in for statement? ES6. or How to remove RubyMine warning?


let

const numbers = [1, 2, 3, 4, 5];

for (let number of numbers) { // prefer-const
  if (number === 2) {
    continue;
  } else if (number == 4) {
    return;
  }
  console.log(number);
}

eslint say

number is never modified, use const instead.(prefer-const)

enter image description here


const

const numbers = [1, 2, 3, 4, 5];

for (const number of numbers) {
  if (number === 2) {
    continue;
  } else if (number == 4) {
    return;
  }
  console.log(number);
}

RubyMine 7.1.4 say

const variable without initializer. It won't be possible to assign meaningful value later.

enter image description here

What should I do? (I think const is ok, remove RubyMine warnings, but how?)


Solution

  • You can use both, it doesn't make much of a difference. Did you plan that it should not be modified? Use const. Do you consider it might need to be modified? Use let. Don't you care? Use either, or what your styleguide recommends, in this case const.

    That RubyMine warning is clearly wrong, they should fix the bug.