Can someone explain me which situation I should use let & const?
Hi. These readings may help:
Most people will tell you never to use
var. I’m not sure if that should be written in stone, but in general it is best to avoid.
Instead, always try to use
const. And then if you end up needing to change the initial value of a variable you can change it to
let. There are some obvious places where you will always have to use
let (e.g. the counter variable in a loop). But I think you will be surprised at just how much you can get away with using
I suggest making
const an abbreviation in your editor (mine is just
c). You’ll be typing it all the time.
constby default every time
letonly when there’s really no way you can use
You might think that it’s just a hype or convention and of course it’s true but it has some optimization behind it as well - as value cannot be changed, parser will insert value everywhere you’re using it saving some time for compiler
- in fact parser will do it with
lettoo, so yeah… just a readability convention really ))