Home>Article>Web Front-end> Understand var, let and const in JS

This article will introduce you to JavaScript's var, let and const. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone. The
varstatement is used to declare a variable in JavaScript, which follows the following rules:
window.When appearing in the global scope,varcreates a global variable. Additionally it creates aglobal propertywith the same name onwindow:
var city = "Florence"; console.log(window.city); // "Florence"
When declared inside a function, the variable is scoped to that function:
var city = "Florence"; function bubble() { var city = "Siena"; console.log(city); } bubble(); // "Siena" console.log(city); // "Florence"
varDeclaration will be promoted:
function bubble() { city = "Siena"; console.log(city); var city; // hoists } bubble(); // "Siena"
A variable assigned without any declaration (eithervar,letorconst) will becomeglobal variablesby default:
function bubble() { city = "Siena"; console.log(window.city); } bubble(); // "Siena"
In order to eliminate this behavior, requires the use ofstrict mode:
"use strict"; function bubble() { city = "Siena"; console.log(window.city); } bubble(); // ReferenceError: assignment to undeclared variable city
Any variable declared withvarcan be made laterReassignorRedeclare. Example of redeclaration:
function bubble() { var city = "Florence"; var city = "Siena"; console.log(city); } bubble(); // "Siena"
Example of reassignment:
function bubble() { var city = "Siena"; city = "Florence"; console.log(city); } bubble(); // "Florence"
letstatement declares a variable in JavaScript that obeys the following Rules:
window.Variables declared withletdo not create any global properties onwindow:
let city = "Florence"; console.log(window.city); // undefined
When declared inside a function, the scope of the variable is the function:
let city = "Florence"; function bubble() { let city = "Siena"; console.log(city); } bubble(); // "Siena" console.log(city); // "Florence"
When declared within theblock, the scope of the variable is the block. Here is an example of use in a block:
let city = "Florence"; { let city = "Siena"; console.log(city); // "Siena"; } console.log(city); // "Florence"
An example with aifblock:
let city = "Florence"; if (true) { let city = "Siena"; console.log(city); // "Siena"; } console.log(city); // "Florence"
Conversely,varis not affected by the block Limitations of:
var city = "Florence"; { var city = "Siena"; console.log(city); // "Siena"; } console.log(window.city); // "Siena"
letThe statement may be promoted, butwill generate a temporary dead zone:
function bubble() { city = "Siena"; console.log(city); // TDZ let city; } bubble(); // ReferenceError: can't access lexical declaration 'city' before initialization
The staging dead zone prevents access to theletstatement before initialization. Another example:
function bubble() { console.log(city); // TDZ let city = "Siena"; } bubble(); // ReferenceError: can't access lexical declaration 'city' before initialization
It can be seen that the exceptions generated in the two examples are the same: proving the emergence of "temporary dead zone".
Any variabledeclared withletcannot be redeclared. Example of a redeclaration that throws an exception:
function bubble() { let city = "Siena"; let city = "Florence"; console.log(city); } bubble(); // SyntaxError: redeclaration of let city
This is an example of a valid redeclaration:
function bubble() { let city = "Siena"; city = "Florence"; console.log(city); } bubble(); // "Florence"
conststatement is used in JavaScript Declare a variable in, which follows the following rules:
window.Variables declared with const will not create any global properties onwindow:
const city = "Florence"; console.log(window.city); // undefined
When inside a function When declared, the scope of the variable is the function:
const city = "Florence"; function bubble() { const city = "Siena"; console.log(city); } bubble(); // "Siena" console.log(city); // "Florence"
When declared in theblock, the scope of the variable is the block. Example of block statement{}:
const city = "Florence"; { const city = "Siena"; console.log(city); // "Siena"; } console.log(city); // "Florence"
Example inifblock:
const city = "Florence"; if (true) { const city = "Siena"; console.log(city); // "Siena"; } console.log(city); // "Florence"
constThe declaration may be promoted, butwill enter the temporary dead zone:
function bubble() { console.log(city); const city = "Siena"; } bubble(); // ReferenceError: can't access lexical declaration 'city' before initialization
UseconstAny variable declaredcannot be redeclared nor reassigned. An example of an exception being thrown on redeclaration:
function bubble() { const city = "Siena"; const city = "Florence"; console.log(city); } bubble(); // SyntaxError: redeclaration of const city
Example of reallocation Example:
function bubble() { const city = "Siena"; city = "Florence"; console.log(city); } bubble(); // TypeError: invalid assignment to const 'city'
| Block scope | Temporary dead zone | Create global properties | Reassignable | Redeclarable | |
|---|---|---|---|---|---|
##var##❌ |
❌ | ✅ | ✅ | ✅ | |
✅ |
✅ | ❌ | ✅ | ❌ | |
##✅ | ✅
❌ | ❌ | ❌ |
Author: ValentinoRelated free learning recommendations:
The above is the detailed content of Understand var, let and const in JS. For more information, please follow other related articles on the PHP Chinese website!