What are the differences between var, let, and const in JavaScript?
var is function-scoped, can be reassigned, hoisted with undefined, and attached to the global window object; 2. let and const are block-scoped, with let allowing reassignment and const not allowing it, though const objects can have mutable properties; 3. let and const are hoisted but not initialized, existing in the Temporal Dead Zone until declared, and are not added to the global window object; 4. Modern best practices recommend using const by default, let when reassignment is needed, and avoiding var due to its broader scope and hoisting behavior, making const > let > var in current standards.

The main differences between var, let, and const in JavaScript come down to scope, hoisting, and whether the variable can be reassigned. Here's a clear breakdown:

1. Scope
-
var: Function-scoped
Variables declared withvarare accessible within the function they're defined in, or globally if outside any function.function example() { if (true) { var x = 10; } console.log(x); // 10 — accessible outside the 'if' block } letandconst: Block-scoped
These are only accessible within the block{}they're defined in (like inside anif,for, or any pair of braces).
function example() { if (true) { let y = 20; } console.log(y); // ReferenceError: y is not defined }
2. Reassignment
varandlet: Can be reassigned
You can change the value after declaration.let name = "Alice"; name = "Bob"; // ✅ Allowed
const: Cannot be reassigned
Must be assigned at declaration, and the variable cannot point to a new value.
const age = 25; age = 30; // ❌ TypeError: Assignment to constant variable
⚠️ Note:
constdoesn't make objects or arrays immutable — their contents can still change.const user = { name: "Alice" }; user.name = "Bob"; // ✅ Allowed — object property changed user = {}; // ❌ Not allowed — reassigning the variable
3. Hoisting and Temporal Dead Zone (TDZ)
var: Hoisted and initialized withundefined
You can access it before declaration (but it returnsundefined).console.log(value); // undefined, not an error var value = 5;
letandconst: Hoisted but not initialized
They exist in the "Temporal Dead Zone" before declaration — accessing them causes aReferenceError.console.log(count); // ReferenceError let count = 1;
So you must declare them before use.
4. Window Object Attachment
var: Adds to the globalwindowobject (in browsers) when declared globally.var apiKey = "123"; console.log(window.apiKey); // "123"
letandconst: Do not add to thewindowobject.let apiKey = "123"; console.log(window.apiKey); // undefined
Summary Table
| Feature | var |
let |
const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Can reassign | Yes | Yes | No |
| Hoisted | Yes (as undefined) |
Yes (TDZ) | Yes (TDZ) |
Global to window
|
Yes | No | No |
| Must initialize | No | No | Yes |
In modern JavaScript, use const by default, and let only when you know the variable will change. Avoid var unless working with legacy code. It's clearer and less error-prone.
Basically, const > let > var in today's best practices.
The above is the detailed content of What are the differences between var, let, and const in JavaScript?. 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)
Solve the 'error: use of undeclared identifier 'variable'' problem in C++ code
Aug 26, 2023 pm 01:46 PM
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
How to use variables in PHP
May 20, 2023 pm 02:33 PM
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: undeclared name 'x' (cannot refer to unexported name), solution steps
Aug 19, 2023 am 11:01 AM
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
Quickly get started with the basic syntax of Golang variable declaration and assignment
Dec 23, 2023 am 08:10 AM
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
Difference between `var`, `let`, and `const` declarations in JavaScript
Jul 08, 2025 am 02:21 AM
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.
Differences Between var, let, and const Declarations in JavaScript
Jul 06, 2025 am 12:58 AM
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
What are the differences between var, let, and const in JavaScript?
Aug 02, 2025 pm 01:30 PM
varisfunction-scoped,canbereassigned,hoistedwithundefined,andattachedtotheglobalwindowobject;2.letandconstareblock-scoped,withletallowingreassignmentandconstnotallowingit,thoughconstobjectscanhavemutableproperties;3.letandconstarehoistedbutnotinitial
Understanding var, let, and const in JavaScript
Jul 12, 2025 am 03:11 AM
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




