variable

Variables are “named stores” of data used to store information.

Variable declarations

Variable declarations can be made in the following ways:

var

var message;
// You can combine variable definitions and assignments on a single line
var message = '1111'
// Declared unassigned values default to undefined and can be accessed by variable names
 var lll;
 var mmm = '1111';
console.log(typeof lll) // undefined
console.log(mmm)/ / '1111'
// Multiple variables can be declared on a single line, or multiple variables can be declared and assigned on a single line (except const)
var lll,eee,ttt;
var lll = '1111',eee='222, TTT ='333'; // Var a can be assigned multiple times except const; a= 1; a=3; Console. log(a) // 3 // Can copy one variable to another variable let a = 3; let b = 4; b = a ; console.log(b) //3Copy the code

let

let message;
Copy the code
  1. There is no variable promotion
 console.log(l)
 const l = 4
 //Cannot access 'l' before initialization
Copy the code
  1. There are temporary dead zones
 let l = 4
  {
    console.log(l)
    let l = 6
  }
// Cannot access 'l' before initialization
Copy the code
  1. Duplicate declarations are not allowed
 var l = 5
 let l = 8
//Identifier 'l' has already been declared
Copy the code
  1. Have block-level scope
{
   let l = 9
}
console.log(l)
 //l is not defined
Copy the code

Const (used to declare constants)

const message;
Copy the code
  1. There is no variable promotion

     console.log(m)
     const m = 4
     //Cannot access 'm' before initialization
    Copy the code
  2. Duplicate declarations are not allowed

        var m = 'Hello! '
        let a = 25
    
         const m = 'Goodbye! '
         const a = 30
         //Identifier 'm' has already been declared
    Copy the code
  3. Have block-level scope

      {
        const m = 9
      }
       console.log(m)
    //m is not defined
    Copy the code
  4. The declaration must be assigned and cannot be modified (for simple data types)

    const PI;
    //Missing initializer in const declaration
    // For simple data types
    const s = 9
    s = 8
    //Assignment to constant variable. 
    // For complex data types
     const obj = {}
     obj.a = 3
    // {a:3}
    Copy the code

    What const actually guarantees is not that the value of the variable cannot be changed, but that the data stored at the memory address to which the variable points cannot be changed. For simple types of data (values, strings, booleans), the value is stored at the memory address the variable points to. But for complex type data (mainly objects and arrays), variable pointing to the memory address, save only a pointer to the actual data, const can guarantee the pointer is fixed (i.e., always points to the other a fixed address), as far as it is pointing to the data structure of variable, is completely unable to control.

Variable naming

Variable naming has the following restrictions:

  • Variable names must contain only letters, numbers, and the symbols $and _.

  • The first character must be a non-digit.

  • Reserved words cannot be used

    / / effective
    let userName;
    let test123;
    / / null and void
    let 11mm
    let class
    Copy the code
  • Case sensitive naming

  • Non-english letters are allowed, but not recommended

    let p
    let P;// Case sensitive
    letI;/ / do not recommend
    Copy the code