current position:Home>Winter vacation javascript cram school Chapter 2 let & const temporary dead zone

Winter vacation javascript cram school Chapter 2 let & const temporary dead zone

2023-01-25 10:50:36small fish programmer

The performance of temporary dead zone

if (true) {

console. log(value);

let value = 1;

}

Uncaught ReferenceError: Cannot access 'value' before initialization

var:
1. In ES5, it is a global variable, a top-level variable (referring to window)

2. There is a case of variable promotion

console.log(a) //undefined
var a=10
This code will be transformed into the following

var a
consolse.log(a)
a=10
3. The statement can be repeated, and the final result is that the last one will overwrite the previous variable

4. Using var in a function is a local variable, and directly using a variable in a function is a global variable (no var)

let:
1. The scope of let is block-level code, which is only valid in code blocks.

2. There is no variable promotion (temporary dead zone)

3. Duplicate definitions are not allowed

const:
1. const declares a read-only constant. Once declared, the value of the constant cannot be changed

2. If you have declared a variable with var or let before, you will also report an error if you declare it with const

3. There is no variable promotion (temporary dead zone)

This also reflects the safety of let and const

copyright notice
author[small fish programmer],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2023/025/202301251040055537.html

Random recommended