Understand var, let and const in JS

青灯夜游
Release: 2020-10-20 17:13:25
forward
2501 people have browsed it

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

var

varstatement is used to declare a variable in JavaScript, which follows the following rules:

  • The scope is a function scope or global scope.
  • Not limited by the temporary dead zone (TDZ).
  • It will create a global property with the same name onwindow.
  • isallocatable.
  • isdeclarable.

Function scope and global scope

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"
Copy after login

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"
Copy after login

varDeclaration will be promoted:

function bubble() { city = "Siena"; console.log(city); var city; // hoists } bubble(); // "Siena"
Copy after login

Unexpected global variable

A variable assigned without any declaration (eithervar,letorconst) will becomeglobal variablesby default:

function bubble() { city = "Siena"; console.log(window.city); } bubble(); // "Siena"
Copy after login

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
Copy after login

Reassignable and redeclared

Any variable declared withvarcan be made laterReassignorRedeclare. Example of redeclaration:

function bubble() { var city = "Florence"; var city = "Siena"; console.log(city); } bubble(); // "Siena"
Copy after login

Example of reassignment:

function bubble() { var city = "Siena"; city = "Florence"; console.log(city); } bubble(); // "Florence"
Copy after login

let

letstatement declares a variable in JavaScript that obeys the following Rules:

  • belongs to block scope.
  • is subject to thetemporary dead zone.
  • It does not create any global properties onwindow.
  • isallocatable.
  • Cannot be redeclared.

Block scope

Variables declared withletdo not create any global properties onwindow:

let city = "Florence"; console.log(window.city); // undefined
Copy after login

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"
Copy after login

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"
Copy after login

An example with aifblock:

let city = "Florence"; if (true) { let city = "Siena"; console.log(city); // "Siena"; } console.log(city); // "Florence"
Copy after login

Conversely,varis not affected by the block Limitations of:

var city = "Florence"; { var city = "Siena"; console.log(city); // "Siena"; } console.log(window.city); // "Siena"
Copy after login

Temporary Dead Zone

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
Copy after login

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
Copy after login

It can be seen that the exceptions generated in the two examples are the same: proving the emergence of "temporary dead zone".

Reassignable, not redeclared

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
Copy after login

This is an example of a valid redeclaration:

function bubble() { let city = "Siena"; city = "Florence"; console.log(city); } bubble(); // "Florence"
Copy after login

const

conststatement is used in JavaScript Declare a variable in, which follows the following rules:

  • belongs to the block scope.
  • is subject to the "temporary dead zone".
  • It does not create any global properties onwindow.
  • Not reassignable.
  • Cannot be redeclared.

Block scope

Variables declared with const will not create any global properties onwindow:

const city = "Florence"; console.log(window.city); // undefined
Copy after login

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"
Copy after login

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"
Copy after login

Example inifblock:

const city = "Florence"; if (true) { const city = "Siena"; console.log(city); // "Siena"; } console.log(city); // "Florence"
Copy after login

Temporary dead zone

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
Copy after login

cannot be reallocated, cannot be redeclared

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
Copy after login

Example of reallocation Example:

function bubble() { const city = "Siena"; city = "Florence"; console.log(city); } bubble(); // TypeError: invalid assignment to const 'city'
Copy after login

Summary

let const ✅ ❌ ❌ ❌ English original address: https://www.valentinog.com/blog/var/

Block scope Temporary dead zone Create global properties Reassignable Redeclarable
##var##❌
##✅
Author: Valentino

Related free learning recommendations:

js video tutorial

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!