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 const
.
I suggest making const
an abbreviation in your editor (mine is just c
). You’ll be typing it all the time.
- Use
const
by default every time - Use
let
only when there’s really no way you can useconst
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
let
too, so yeah… just a readability convention really ))