Search code examples
javascriptnode.jslintstandardjs

StandardJS not recognising array.push() as reassignment


I've declared an empty array before a loop that then gets populated as the loop runs. My code works fine but when I lint using StandardJS it tells me that the array is never reassigned and should be declared as a const. If i did this then I wouldn't be able to add values to my array and my code wouldn't work. This also means i cant use standard --fix because it breaks my code.

let primeFactors = []
while (number > 1) {
  if (isPrime(divisor)) {
    if (number % divisor === 0) {
      primeFactors.push(divisor)
      number = number / divisor
    } else {
      divisor++
    }
  } else {
    divisor++
  }
}

Am i missing something here?


Solution

  • You can, and should use const.

    The name const is a bit of a misnomer - it is merely saying that the variable, once declared, cannot be redeclared or reassigned - that is, it always points at the same object in memory (or more generally, is always the same value as when it was first created) For further explanation, see this post on MDN.

    Note however that the object can be mutable, even if it's declared as const. Just because primeFactors is always pointing at the same array doesn't mean that array can't grow/shrink/change.

    In my javascript, very few of my variables are let - I use const pretty much everywhere, which makes code much easier to read and reason about. (The only times I use let is the occasional for each loops, and occasional helper algorithmic methods)

    So the linter is technically right, and encouraging best practice - For example, the linter is protecting you from code such as the following - which would cause eg an Uncaught TypeError: Cannot read property 'push' of null:

    let primeFactors = []
    while (number > 1) {
      if (isPrime(divisor)) {
        if (number % divisor === 0) {
          primeFactors.push(divisor)
          number = number / divisor
        } else {
          divisor++
          primeFactors = null // There's no reason to do this, but it's theoretically possible!
        }
      } else {
        divisor++
      }
    }