In my opinion nowadays it is better to use let and const.
According to the ES6 standard, these options are more convenient with their scope. Both give us the so-called block scope. This means that the scope of their use can be limited to a block of code, such as a for loop or an if expression. This gives the developer more flexibility in choosing the scopes of variables.
In addition, if we consider these options, then you will not be able to change the variable declared with const, unlike var and let.
Generally speaking, let and const are often used now, depending on the need.
Answer from nixanosize on Stack OverflowDifference between var, let and const
When should you use "var", "let", or "const" in JavaScript code - Stack Overflow
javascript - How much should I be using 'let' vs 'const' in ES6? - Software Engineering Stack Exchange
JavaScript variables: var and let and const
Videos
Hi everyone!
I'm pretty new to web development and have started to read a little bit about javascript. Right now I'm trying to learn how to declare a function but realized that there is different ways to do this using var, let or const. Since I'm new to this, most of the explanation on the internet is pretty advanced to understand atm, so could anyone explain the differences for me very short short and easy, alternatively give me a link that explain it in pretty easy terms?
Thanks in advance!
(Edit: Thank you so much for the answers, rly appreciate it!)
My reply here is not javascript-specific.
As a rule of thumb in any language that lets me do so in a semi-easy way I'd say always use const/final/readonly/whatever it is called in your language whenever possible. The reason is simple, it's much easier to reason about code when it is dead obvious what can change and what cannot change. And in addition to this, in many languages you can get tool support that tells you that you are doing something wrong when you accidentially assign to a variable that you've declared as const.
Going back and changing a const to a let is dead simple. And going const by default makes you think twice before doing so. And this is in many cases a good thing.
How many bugs have you seen that involved variables changing unexpectedly? I'd guess a lot. I know that the majority of bugs that I see involve unexpected state changes. You won't get rid of all of these bugs by liberally using const, but you will get rid of a lot of them!
Also, many functional languages have immutable variables where all variables are const by default. Look at Erlang for example, or F#. Coding without assignment works perfectly in these languages and is one of the many reasons why people love functional programming. There is a lot to learn from these languages about managing state in order to become a better programmer.
And it all starts with being extremely liberal with const! ;) It's just two more characters to write compared to let, so go ahead and const all the things!
Be careful, because const object keys are mutable.
From here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
object keys are not protected
consider this example:
const colors = {red: "#f00"};
console.log(colors); // { "red": "#f00" }
colors.red = "#00f";
colors.green = "#0f0";
console.log(colors); // { "red": "#00f", "green": "#0f0" }
Same thing for arrays:
const numbers = [1, 2, 3];
console.log(numbers); // [ 1, 2, 3 ]
numbers.push(4);
console.log(numbers); // [ 1, 2, 3, 4 ]
I haven't decided totally myself, but I'm considering using const for all non-array/non-objects and use let for objects/arrays.