Difference between Javascript Var, Let, and Const

Var, let, and const are all keywords in JavaScript that are used to declare variables. However, they have different properties and use cases.

Var

Let (ES6)

Const (ES6)

Here is a table that summarizes the key differences between var, let, and const:

Keyword Scope Re-declarable? Updatable? Hoisted?
Var Function Yes Yes Yes
Let Block Yes No No
Const Block No No No

When to use each keyword

It is generally recommended to use let and const whenever possible, and to avoid using var unless necessary. This is because let and const are more predictable and less likely to cause errors.