There are three keywords in JavaScript that can be used to declare variables: let, var, and const. Each keyword has different rules and implications for how the variables they create can be used.
What is difference between var, let and const?
The var keyword is the oldest way of declaring variables in JavaScript and is supported by all browsers. The let and const keywords are newer additions to the language and are not supported by older browsers. If you need to support older browsers, you can use var instead of let or const. If you don’t need to support older browsers, you can use let or const. If you want your variable to be immutable, use const. Here are some examples:
As you can see, var and let variables can be reassigned, but const variables can not. Another difference between var and let/const is that var variables are function-scoped, while let and const variables are block-scoped. This means that var variables are only available within the function they were declared in. For example:
On the other hand, let and const variables are only available within the block they were declared in. For example:
So, to sum up, the main differences between var, let and const are:
var is function-scoped while let and const are block-scoped. var variables can be reassigned while let and const variables can not. var variables are declared using the var keyword while let and const variables are declared using the let and const keywords respectively. const variables are immutable while let and var variables are not.