The Difference Between `let`, `const`, and `var` Explained in Depth
Jul 27, 2025 am 01:44 AMconst is preferred by default as it prevents reassignment and signals intent clearly; 2. use let when reassignment is needed, such as in loops or changing state; 3. avoid var due to its function-scoping, hoisting issues, and potential for bugs; 4. let and const are block-scoped and hoisted in a Temporal Dead Zone, making them safer and more predictable than var; 5. const does not make objects or arrays immutable—only the binding is protected, so properties can still be modified—all these differences ensure cleaner, more maintainable code when followed properly.
When writing JavaScript, you’ll often need to declare variables — and you’ve got three keywords to choose from: var
, let
, and const
. While they all serve the purpose of storing values, they behave very differently under the hood. Understanding these differences is crucial for writing predictable, bug-free code.

Let’s break down each one and explore their scoping, hoisting behavior, reassignment rules, and real-world implications.
1. Scope: Where Your Variables Live
The most important difference between var
, let
, and const
is scope — that is, where in your code a variable can be accessed.

-
var
is function-scoped
If you declare a variable withvar
inside a function, it’s only accessible within that function. But if you declare it inside a block (like anif
statement orfor
loop), it’s still accessible outside the block — as long as it’s in the same function or global scope.if (true) { var x = 10; } console.log(x); // 10 — accessible!
let
andconst
are block-scoped
They only exist within the block (anything inside{}
) where they’re defined.if (true) { let y = 20; const z = 30; } console.log(y); // ReferenceError: y is not defined console.log(z); // ReferenceError: z is not defined
This makes let
and const
much safer — they prevent accidental access from outside the intended scope.
2. Hoisting: How Variables Are Lifted During Compilation
JavaScript moves all variable declarations to the top of their scope during the compilation phase — this is called hoisting. But how each keyword behaves when hoisted is different.
var
is hoisted and initialized withundefined
You can access avar
variable before it’s declared — but it will beundefined
.console.log(a); // undefined var a = 5;
This can lead to confusing bugs because the variable exists but holds no meaningful value yet.
let
andconst
are hoisted but not initialized
They exist in a special zone called the Temporal Dead Zone (TDZ) until their declaration is reached. Accessing them before declaration throws aReferenceError
.console.log(b); // ReferenceError: Cannot access 'b' before initialization let b = 5;
This enforces better coding practices by preventing the use of variables before they’re declared.
3. Reassignment and Mutability
This is where let
and const
differ in intent, even though both are block-scoped.
let
allows reassignment
You can change the value of alet
variable as many times as needed.let count = 1; count = 2; // ? OK
const
does not allow reassignment
Once you assign a value to aconst
, you can’t assign a new one.const name = "Alice"; name = "Bob"; // TypeError: Assignment to constant variable
?? Important:
const
doesn’t make the value immutable — it only protects the assignment. If the value is an object or array, its contents can still be modified.const user = { name: "Alice" }; user.name = "Bob"; // ? OK — object property changed user.age = 25; // ? OK — new property added user = {}; // ? TypeError — can't reassign the variable
So const
means “this variable will always point to the same value,” not “the value itself is frozen.”
4. When to Use Which? Best Practices
Now that we’ve seen the technical differences, here’s how to use them effectively:
Use
const
by default
If you don’t plan to reassign the variable (which is most cases), useconst
. It makes your intent clear and prevents accidental reassignment.Use
let
when you need to reassign
Loop counters, accumulators, or flags that change state are good candidates.for (let i = 0; i < 10; i ) { ... }
Avoid
var
in modern JavaScript
It’s outdated in most contexts due to its loose scoping and hoisting quirks. The only reason to usevar
today is if you're working in an old environment that doesn’t supportlet
/const
(very rare now).
Summary of Key Differences
Feature | var |
let |
const |
---|---|---|---|
Scope | Function-scoped | Block-scoped | Block-scoped |
Hoisting | Hoisted, undefined
|
Hoisted, TDZ | Hoisted, TDZ |
Reassignment | Allowed | Allowed | Not allowed |
Redeclaration | Allowed (in same scope) | Not allowed | Not allowed |
Temporal Dead Zone | No | Yes | Yes |
? Note: Redeclaring a variable with
var
in the same scope is allowed (though dangerous), butlet
andconst
will throw an error if you try to redeclare.
Basically, stick with const
unless you need reassignment — then use let
. Forget var
. It’s that simple.
The above is the detailed content of The Difference Between `let`, `const`, and `var` Explained in Depth. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solving the "error:useofundeclaredidentifier'variable'" problem in C++ code When programming in C++, we often encounter various errors. One of the common errors is "error:useofundeclaredidentifier'variable'". This error usually means that we are using an undeclared variable in our code. This article will detail

PHP is a very popular web development language that allows developers to create dynamic web applications on the server side. In PHP, a variable is a basic data structure used to store values ??and data. This article will introduce how to use variables in PHP. Basic Syntax of Variables The syntax for declaring variables in PHP is very simple. Variable names begin with a dollar sign ($), followed by the variable name. Variable names can be a combination of letters, numbers, or underscores, but they must begin with a letter or an underscore. For example, the following code declares a name

How to solve golang error: undeclaredname'x'(cannotrefertounexportedname), solution steps. During the development process of using Golang, we often encounter various error messages. One of the common errors is "undeclaredname'x'(cannotrefertounexportedname)" which refers to the variable

Quick introduction to the basic syntax overview of Golang variable declaration and assignment: Golang is a statically typed, compiled programming language with excellent performance and development efficiency. In Golang, variable declaration and assignment are one of the basic operations we often use when writing programs. This article will take you quickly into the basic syntax of Golang variable declaration and assignment, and provide specific code examples. Variable declaration: In Golang, we need to use the var keyword to declare a variable and specify the type of the variable. Change

In JavaScript, the main differences between var, let and const are scope, enhancement behavior, and reassignability. var is the function scope, while let and const are block scope; variables declared by var will be promoted and initialized to undefined, while let and const are promoted but will not be initialized, and access will be errors (temporary dead zone); var and let allow reassignment, while const does not allow reassignment, but the content of the mutable object can be modified. When using it, use const first. Use let when reassigning values ??to avoid using var.

In JavaScript, var is the function scope, and let and const are block scopes; var allows variables to be promoted and initialized to undefined, while let and const are promoted but not initialized, and access will be errors; variables declared by const cannot be reassigned, but the contents of objects or arrays are variable. Specifically: 1.var is the function scope, and if declared outside the function, it is the global scope; 2.let and const are block scopes, which are only valid in the declared block; 3.var and let allow reassignment, const does not allow reassignment, but the object or array content they refer to can be modified; 4.var variables will be promoted and initialized to undefined, let

Var, let, and const in JavaScript have significant differences in scope, variable promotion, and variability. 1.var is the function scope, accessible anywhere within the function; let and const are block-level scopes, accessible only within the code block that declares them. 2. Var has variable promotion and is initialized to undefined. Although let and const are also promoted, they are in a "temporary dead zone". An error will be reported before the declaration. 3. Var can be repeatedly declared and assigned; let cannot be repeatedly declared but can be reassigned; const cannot be repeatedly declared and cannot be reassigned (object or array content can be modified). 4. Const should be used first in actual development, if reassignment is required

constispreferredbydefaultasitpreventsreassignmentandsignalsintentclearly;2.useletwhenreassignmentisneeded,suchasinloopsorchangingstate;3.avoidvarduetoitsfunction-scoping,hoistingissues,andpotentialforbugs;4.letandconstareblock-scopedandhoistedinaTemp
