koalaok
koalaok

Reputation: 5760

Why ESLint prefer-const wants object or array defined as const?

Take this lines as examples:

let c=[]
c[0]=23

and these:

let creationDate = new Date(date)
creationDate.setDate(creationDate.getDate() + daysTTL);

Both let statements will raise ESLint ERROR

Identifier .... is never reassigned; use 'const' instead of 'let'.

Is there a better way to instruct ESLint that I'm actually changing the values, or that this rules doesn't apply/fit well with structured data?

Or should I switch to const ?

Upvotes: 4

Views: 2651

Answers (1)

Morhaf Shamia
Morhaf Shamia

Reputation: 382

actually the issue raised be ESLint is due to small difference, whereby const is used for variables that are never reassigned using the = while let for variables that will be surely be reassigned using =.

let me explain using your two examples, the first example the variable c was not reassigned, just the value of the its element has been changed or assigned so the reassignment was not performed on c but on its element c[0]

regarding the second example, you are changing the value of a property of an object but you are not reassigning that object to another or to new.

whether to use const or let it depends on whether you will ever reassign using =.

hope this was helpful

Upvotes: 5

Related Questions