About the difference between let and const in JavaScript (notes)

Table of contents

  1. Main differences:
  2. let generally declares variables
  3. const is generally used to declare variables, arrays, and objects

Main differences:

let:
General declaration of variables
const:
Generally used to declare constants, arrays, and objects (the address value cannot be modified)

let generally declares variables

1. Declare variables
Example: let a;
        let b,c;
2. Variables cannot be declared repeatedly
Example: let start = 'aa'
       let start ='bb'    X
3. Block-level scope
 example:{
        let c = 'Hello'
        }      
        console.log(c)   X  cannot be quoted
4. There is no variable promotion
Example: console.log(song)
        let song = 'La La La'   the result is underfined
5.  Does not affect scope effects
example:
        {
        let school = 'Agricultural University'
        function fu(){
        console.log(school)

        }}

const is generally used to declare variables, arrays, and objects

1. Be sure to assign an initial value
Example: const A = 'Hello'
2. Generally use capital letters for constants (writing standards)
Example: const B = 'uppercase'
3. The value of a constant cannot be modified.
Example: A = 'Haha'          X
4. Block-level scope
example:{
        const O = 'UZI'
 }
5. Modification of elements of arrays and objects does not count as modification of constants, and no error is reported (because you change the address value)
Example: const TEST = ['a','b']
        TEST.push('c')

Guess you like

Origin blog.csdn.net/m0_69097184/article/details/132586428