← Back to list

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…

Tentorbest · 2026-03-28 04:07 · 1 claps · 1.1 min read
#javascript-variable #programming #coding #javascript
Open on Medium ↗
Wiki topics: 💻 · Programming 🌐 · Web Development ☁️ · DevOps & Cloud

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:

  1. 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”

  1. 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;

  1. var and let can be reassigned but not const.
  2. var can be redeclared within the same scope but let and const cannot be redeclared within the same scope.
  3. 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