Difference between let, const and var in JavaScript
A variable declaration is tells the computer that a variable exists and what value it should start with. Similar to other programming languages JavaScript also allows for the declaration of variables.
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.
The let keyword is used to declare variables in JavaScript. The var keyword can also be used to declare variables, likewise the const but the key difference between them lies in their scopes.
Let Keyword
The let keyword declares a block-scoped local variable, optionally initializing it to a value.
Block-scoped means that the variable is only available within the block it was declared in, which is usually denoted by curly braces {}.
Var Keyword
The var keyword declares a function-scoped or global variable, optionally initializing it to a value.
Get Favour Ezekwu’s stories in your inbox
Join Medium for free to get updates from this writer.
Function-scoped means that the variable is only available within the function it was declared in. Global variables are available throughout your entire code.
Const Keyword
The const keyword declares a block-scoped, immutable constant variable, i.e. a variable that can’t be reassigned.
Constants are also called “immutable variables”, but that’s a bit of a misnomer since they are actually variables — just ones that can’t be reassigned.
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.





