What are the Differences Between var, let, and const?
When no keyword is used, the variable will be assigned to the global object which is the
This is the default: it creates a function scoped variable which can be reassigned or redeclared later on.
This was introduced in ES6 and it is the preferred way over using
var. It creates a block-scoped variable which can be reassigned but can’t be redeclared.
It was introduced in ES6 along with
let. It also creates a block-scoped variable but this can’t be reassigned nor redeclared.
const should be used when you don’t want to reassign a value later on.
It’s also good to mention that all declarations will be hoisted to the top of their scope.
var should be avoided if can, use
- Access exclusive interactive lessons
- Unlimited access to hundreds of tutorials
- Remove ads to learn without distractions