...

/

Variable Declarations with let and const

Variable Declarations with let and const

Learn about the behavior of "var", "let", and "const" and the differences between them.

For many years, we could only use var to declare a variable in JavaScript. Since 2015 however, JavaScript has gained two new keywords that we can use to declare variables: let and const. Using var for variable declarations has become somewhat superfluous and in almost all cases, let and const are better choices. But what is the difference?

As opposed to var, the new variable declarations, let and const, only exist inside of the scope in which they were defined. These scopes can be a ...