ESLint says the array never changed even if elements are inserted into the array

I am converting some existing code to follow an ECMA script, and I am using ESLint to follow the coding standard. I have the following ecacccript method

static getArrayOfIndices(text, char) { let resultArray = []; let index = text.indexOf(char); const lastIndex = text.lastIndexOf(char); while (index <= lastIndex && index !== -1) { resultArray.push(index); if (index < lastIndex) { index = text.substr(index + 1).indexOf(char) + index + 1; } else { index = lastIndex + 1999; // some random addition to fail test condition on next iteration } } return resultArray; } 

ESLint throws error for resultArray declaration

 ESLint: `resultArray` is never modified, use `const`instead. (prefer-const) 

But since elements are inserted into an array, doesn't that change?

+6
source share
1 answer

To understand this error, you must understand that the declared const variables contain read-only read links. But this does not mean that the value that it has is unchanged [mdn article] .

Since you only change the members of the variable, but do not reassign when binding, the prefer-const rule for es-lint warns you that the declared const variable can be used instead of the declared variable let .

+15
source

All Articles