“This is the second day of my participation in the August More Text Challenge.

There are many ways to name variables, and today we are going to talk about six of them in JavaScript.

var

I think one of the things that stays with us most of the time is using var to name variables.

However, using var to name variables has the following problems:

  • Contaminate global variables, usedvarNamed variables will be mounted globally
var demo = 'demo';
console.log(window.demo === demo); // true
Copy the code
  • Will be promoted
console.log(a); // undefined
var a = 123;
console.log(a); / / 123
Copy the code
  • The type of a variable is determined by its value (can override the original value)
var data = [1.2.3];
console.log(data); / / [1, 2, 3]
data = 123;
console.log(data); / / 123
Copy the code
  • Allow duplicate declarations
var a = 123;
console.log(a); / / 123
var a = [1.2.3];
console.log(a); / / [1, 2, 3].
Copy the code

Due to various problems with var naming variables, ES6 introduced two new naming methods, let and const, and introduced the concept of block-level scope.

let

  • Does not pollute global variables
let demo = 'demo';

console.log(window.demo); // undefined
Copy the code

Variables declared using var are mounted on the window object, but variables declared using let are not mounted on the window object, so there is no contamination of the variables of the global object.

  • There will be no variable promotion
console.log(demo); // Uncaught ReferenceError: Cannot access 'demo' before initialization

let demo = 123;
Copy the code

Unlike var, an error is reported when used before the variable is declared with let. This variable must be initialized before it can be accessed.

  • Duplicate declarations are not allowed
let demo = 'demo';
let demo = 132;
console.log(demo); // Uncaught SyntaxError: Identifier 'demo' has already been declared
Copy the code

You can see on the console that we got an error, and the error message is to tell us that the variable demo has already been declared.

const

Const has all the characteristics of let. I won’t elaborate on it.

A variable named const is usually called a constant. Why? Because after a variable is named const, its value cannot be changed.

In the case of a reference value, const only ensures that the address to which the variable refers is unchanged. It does not guarantee that the reference value of the property inside the variable cannot be changed. As follows:

  • The original value
const demo = 123;
demo = 234;
console.log(demo); // Uncaught TypeError: Assignment to constant variable.
Copy the code

A type error is reported telling us that a constant cannot be reassigned.

  • Reference value
/ / 1
const demo = {a: 1};
demo.a = 2;
console.log(demo); //{a:2}

/ / = = = = = =

/ / 2
const demo = [1];
demo = {a:1};
console.log(demo); // Uncaught TypeError: Assignment to constant variable.
Copy the code

An error is reported whenever a const variable pointing to an address is changed.

Pay attention to

  • useconstWhen naming a variable, be sure to initialize it at the time of naming, otherwise an error will be reported.
  • In a loop, variables that control the loop cannot be usedconst, you can uselet:
    for(const i = 0, i < 9; i ++){
        const a = i;
        console.log(a);
    };
    // 0 Outputs the first time
    // Uncaught TypeError: Assignment to constant variable. Second output, and no further operations
    
    
    for(let i = 0; i < 9; i ++) {
        const a = i;
        console.log(a)
    }
    / / 0
    / / 1
    / / 2
    // ...
    / / 8
    Copy the code