Var vs Let vs Const: What’s the Difference?
See how var vs let vs const differ in JavaScript and when to use each one so your variables stay predictable.

Contents

When we write our JavaScript code, we come across the keywords VAR, LET and CONST. Even though their job is to declare variables, there are actually differences between these declarations.
Let’s now go through them one by one;
VAR
Variables declared with var have a wider scope than the others. We can explain them as follows.
- Variables declared with var can be changed later
- Variables declared with var can be used anywhere in the code and more than once. Or they can be redeclared.
- Variables declared with var are in the scope of functions (function scope), meaning variables declared with var inside a function cannot be accessed from outside the function.
var isim = 'Serhat';
console.log(isim); // the isim variable returns Serhat
function yasKac (_yas) {
var yas = _yas;
console.log(yas);
} yasKac(40);
console.log(yas); // the answer returned will be undefined
LET
Variables declared with let can only be reached within the scope where they are declared. By block scope we mean the inside of each pair of curly braces ({}). They can be changed again later. They can be declared only once within the same scope. When you try to redeclare them, the code throws an error and stops running.
let x = 10; // x variable declared in global scope
if (true) {
let x = 20; // x variable declared in block scope
console.log(x); // output: 20
}
console.log(x); // output: 10
In this example, first the x variable is declared in global scope and assigned the value 10. Then, inside the if block, a new x variable is declared with the let keyword and assigned the value 20. However, this variable is only valid within the block scope, meaning the x variable outside the block has not changed and its value is still 10.
In addition, variables declared with let are only accessible within the scope where they are declared (here, the block scope) and they can be declared only once within the same scope. If you try to declare them again in the same scope, the code will throw an error and stop running.
CONST
A variable declared with const, just like with let, is accessible within the scope where it is declared (block scope), and access from outside it is not possible. The word const actually means Constant, that is, fixed. Within the scope where it is used, it is constant and cannot be changed.
const pi = 3.14; // constant variable declared in global scope
if (true) {
const pi = 3.141592; // constant variable declared in block scope
console.log(pi); // output: 3.141592
}
console.log(pi); // output: 3.14
In this example, first the pi constant variable is declared in global scope and assigned the value 3.14. Then, inside the if block, a new pi constant variable is declared with the const keyword and assigned the value 3.141592. However, this variable is only valid within the block scope, meaning the pi variable outside the block has not changed and its value is still 3.14.
In addition, variables declared with const are only accessible within the scope where they are declared (here, the block scope) and they can be declared only once within the same scope. However, unlike let, variables declared with const are constant and cannot be changed. If you try to change them, the code will throw an error and stop running. For this reason, variables declared with const are generally used for constant values that should not change within the program.
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Redeclaration | Yes | No | No |
| Mutability | Yes | Yes | No |
| Initial Value Required | No | No | Yes |
The table above compares features of the var, let and const keywords such as scope, ability to be redeclared, mutability and the requirement to assign an initial value.
- Scope: The
varkeyword is accessible throughout the function or file that contains the scope where it is declared, while theletandconstkeywords are only valid within the block scope where they are declared. - Redeclaration: A variable declared with
vardoes not throw an error when it is redeclared in the same scope, while theletandconstkeywords can be declared only once and throw an error when they are redeclared in the same scope. - Mutability: Variables declared with the
varandletkeywords can be changed, while variables declared withconstare constant and cannot be changed. - Initial Value Required: Variables declared with
letandconstmust be assigned a value at the moment they are declared. However, there is no such requirement for variables declared withvar.
These are the differences between Var, Let and Const … I hope it has been useful.
Share it if it helped


