var

  • If a variable is declared with the keyword var, it belongs to the current function scope, and if the declaration is a top-level declaration that occurs outside of any function, it belongs to the global scope

  • We can define variables inside functions, and we can access the same variables inside other functions

function f() {
    var a = 10;
    return function g() {
        var b = a + 1;
        returnb; }}var g = f();
g(); // returns 11;
Copy the code
  • In the above example, function G can fetch the variable A defined in function F. Whenever g is called, it has access to the a variable in f. Even when g is called after F has already executed, it can still access and modify A
function f() {
    var a = 1;
    a = 2;
    var b = g();
    a = 3;

    return b;

    function g() {
        return a;
    }
}

f(); // returns 2
Copy the code
  • The variable x is defined inside the if statement, but we can access it outside the statement. This is because the VAR declaration can be accessed anywhere within the function, module, namespace, or global scope that contains it
function f(flag: boolean) {
    if (flag) {
        var x = 10;
    }
    return x;
}

f(true);  // return '10'
f(false); // return 'undefined'
Copy the code
  • These rules can cause some errors. One of them is that declaring the same variable more than once does not give an error
function sumMatrix(matrix: number[] []) {
    var sum = 0;
    for (var i = 0; i < matrix.length; i++) {
        var currentRow = matrix[i];
        for (var i = 0; i < currentRow.length; i++) { sum += currentRow[i]; }}return sum;
}
Copy the code
for (var i = 0; i < 10; i++) {
    setTimeout(function() { console.log(i); }, 100 * i);
}
Copy the code
  • The final output is 10 10s
  • Each function expression passed to setTimeout actually refers to the same I in the same scope, and setTimeout executes a function several milliseconds later, after the for loop ends. After the for loop ends, the value of I is 10. So when this function is called, it prints 10

let

  • When you declare a variable with let, it uses block scope. Unlike variables declared with VAR, which can be accessed outside the function that contains them, block-scoped variables are not accessible outside the block or for loop that contains them
function f(flag: boolean) {
    let a = 100;

    if (flag) {
        let b = a + 1;
        return b;
    }

    // Error: 'b' doesn't exist here
    return b;
}
Copy the code
  • There are two variables defined here, A and b. A is scoped inside f, and B is scoped inside the if block
  • Another feature of variables that have block-level scope is that they cannot be read or written before they are declared. Although these variables always “live” in their scope, the region until the code that declares them is a temporary dead zone

const

  • Const and let have the same scoped rules, but they cannot be reassigned.
  • The internal state of a const variable is modifiable
const age = 9;
const user = {
    name: "bob".age: age,
}

// Error
user = {
    name: "Kitty".age: age
};

// all "okay"
kitty.name = "Rory";
kitty.name = "Cat";
kitty.age--;
Copy the code