Why does this work with a const, but not a let? Even if the variable was reassigned, it should still work.
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
3 messages · Page 1 of 1 (latest)
Why does this work with a const, but not a let? Even if the variable was reassigned, it should still work.
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
@analog owl Here's a shortened URL of your playground link! You can remove the full link from your message.
Preview:```ts
const array = [1, 2, null]
const index = 5
array[index] ||= 0
array[index] += 3
let indexB = 5
array[indexB] ||= 0
array[indexB] += 3```
In the case of const index = 5, TS knows that it cannot change, and array[index] ||= 0 ensures that element cannot be null, so the latter += 3 is allowed.