Search⌘ K

Variable Declarations with let and const

Explore how let and const improve variable scope management and constant references in JavaScript. Understand their differences from var and how to use them for safer, more predictable React components.

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 function, as was the case with var, but they can also be a loop or an if statement.

Note: Whenever you find an open ...