Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 386
koalaok Avatar asked Sep 06 '25 14:09

koalaok


1 Answers

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

like image 90
Morhaf Shamia Avatar answered Sep 11 '25 01:09

Morhaf Shamia