+ 1

Javascript: let, const and var?

Should I create variables with let and const instead of var keyword? I know what's the difference between them but I just don't understand why those two keywords exist... For example, if the value of a variable shouldn't change, we could use const, but we also can simply use var and don't change the value of the variable into the code and then everything's fine...

21st Feb 2018, 10:16 PM
Apple
1 Answer
+ 8
If the value of a variable is never meant to be changed in your code const is more appropriate to use as it cannot be changed accidentally and it also conveys your intent to anyone else reading the code.
21st Feb 2018, 10:38 PM
jay
jay - avatar