Difference Between let, const, and var in JavaScript
Variables are one of the most fundamental concepts in JavaScript, they are containers for storing data values that can be used and…
Difference Between let, const, and var in JavaScript
Variables are one of the most fundamental concepts in JavaScript, they are containers for storing data values that can be used and manipulated throughout a program.
The three main ways JavaScript declares variables are:
- var ; this is the old way of declaring variables before let and const . It declares function-scoped(i.e. inside a specific function) or globally-scoped(i.e. outside any specific function) variables, optionally initializing each to a value.
This can be run;
var x = 5; var y = 6; var total = x + y; // total will be 11
2. let ; this is the modern way of declaring variable and it cannot be redeclared unlike var that can be redeclared. It is used to declare a re-assignable, block-scoped local variable i.e. only accessible within the specific code block in the curly braces.
let username; username = “Peace”; console.log(username); // Output: “Peace”
- const; Just like the name implies, “constant” a constant cannot be changed. A const variable must be assigned a value when it is declared. It cannot be declared first and assigned later .
This can be declared by;
// Correct const PI = 3.14159; const PI = 4;
// Incorrect (will throw a SyntaxError: Missing initializer in const declaration)
This example shows that a value must be assigned while declaring a const, and this assigned value cannot be changed nor redeclared within the same scope.
CONCLUSION
The var, let and const differs from each other in that;
- var and let can be reassigned but not const.
- var can be redeclared within the same scope but let and const cannot be redeclared within the same scope.
- var can be hoisted and initialized with undefined but not let nor const.
메타데이터
- post_id
- ca523b0d8ef1
- slug
- difference-between-let-const-and-var-in-javascript-ca523b0d8ef1
- url
- https://medium.com/@tentorbest/difference-between-let-const-and-var-in-javascript-ca523b0d8ef1
- canonical_url
- https://medium.com/@tentorbest/difference-between-let-const-and-var-in-javascript-ca523b0d8ef1
- author_url
- https://medium.com/@tentorbest
- status
- ok
- fetched_at
- 2026-08-06 06:21:13