The goal of the course, to get this answer, you know why

var let const

Var declares variables

After declaring a variable, the state is raised and it becomes a global variable, a member variable of the window

var name = 'Jump Code Education'
console.log(name)
console.log(window.name)
Copy the code

Declare variables inside functions (using var)

// The function is not executed
function test1() {
    var com = 'Jump Code Education'
}
console.log(com)
//index.js:10 Uncaught ReferenceError: com is not defined
  // at index.js:10
Copy the code

The var inside the function declares a variable, not as a global variable, that is, there is no state promotion

function test1() {
    var com = 'Jump Code Education'
}
test1()
    / / why
    // Uncaught ReferenceError: com is not defined
console.log(com)
Copy the code
Function test1() {function test1() {function test1() {function test1() {function test1();Copy the code

Variables declared inside a function that are not declared using VART are raised by the state gate of the scope and become global variables

function test1() {
    // Initializes variables without keyword declarations
    // become a global variable
    com = 'Jump Code Education'
}
test1()
    // We can do this for you
console.log(com)
console.log(window.com)

// Variable scope enhancement
Copy the code

The following two examples are equivalent

// Variable scope enhancement
// Variables have block-level scope and global scope
// Window is a global object, and its member variables are global scoped
// Why is there no error
function test2() {
    console.log(age)
    var age = 10
}
test2() //undefined
Copy the code
// Promote the declared variable to the top of the function's internal scope
function test2() {
    var age
    console.log(age)
    age = 10
}
test2() //undefined
Copy the code

Var summary

  • Variables declared by var may become global scopes
  • Promotion to the top-level scope inside the function

Let declaration variable

// The scope of the let declaration is local
// The scope cannot go beyond the code block it is in
// The function is destroyed as soon as it completes execution
function test1() {
    let hanyun = 'Jump Code Education';
}
test1()
    // Uncaught ReferenceError: hanyun is not defined
console.log(hanyun)
Copy the code

Classic topic

/** * index.js:56 Uncaught ReferenceError: i is not defined at index.js:56 (anonymous) @ index.js:56 (index):37 Live reload enabled. * */
// The for loop is a block-level scope,
// Each loop generates an example of I and initializes it
// Finally execute I to destroy
for (let i = 0; i < 5; i++) {
    console.log(i)
}
console.log(i) //i is not defined
Copy the code

Var declares global variables

for (var j = 0; j < 5; j++) {
    console.log(window.j)
        // console.log(j)
}
console.log(j) / / 5
    // Why?
console.log(window.j)
Copy the code

Const declaration variable

//const declares a variable
// Const must be initialized
const com = { name: 'Jump Code Education' }
    // const com
console.log(com)
    //const declared variables are immutable and data types cannot be changed
    // com = 1 // this is an error
Copy the code

A variable declared by const can change the value of its member and add member variables

const com = { name: } //const com console.log(com) //const declared variables are immutable, // com.name += com.name console.log(com)Copy the code

Adding member variables

com.age = 1
    //age: 1
    // name: "skyph"
console.log(com)
Copy the code

Avoid var. Const and let are recommended

// Why?
for (var i = 0; i < 5; i++) {
    setTimeout(() = > {
        console.log(i)
    }, 1000)}console.log(window.i)
Copy the code
/ / use the let
for (let i = 0; i < 5; i++) {
    setTimeout(() = > {
        console.log(i)
    }, 1000)}Copy the code

Use anonymous functions

for (var i = 0; i < 5; i++) {
   
}
Copy the code