当前位置:网站首页>Differences among let, VaR and const when JS declares variables

Differences among let, VaR and const when JS declares variables

2022-06-13 08:24:00 tingkeaiii

let、const yes es6 The new method of defining variables .let Has been widely used , It can make up for var Some deficiencies in use .

1.var 

  • var The scope of is global scope and function scope ( Local )
  • var Declared variables , Variable values can be modified , Can be restated
    var v2 = 1;
    var v2 = 2;
    console.log(v2);  /*2,var Can be declared repeatedly */
  • Use var Variables defined , It will automatically become window Object properties , Both in the global scope and in the local scope .let、const Can't
      var v2 = 2;
      console.log(window.v2); /*2,var Global variables are automatically added window Properties of variables */
      let v3 = 1;
      console.log(window.v3); /*undefined*/
  •   var When you're ascending , Declarations and functions are mentioned at the top of the scope , And assign... To the variable undefined The initial value of the .
      function func() {
        console.log(name);
        var name = 'fkit';/*var name Advance and assign initial value , Output undefined*/
        console.log(name);
      }

3. let

  • let A scope is a block level scope ,{ } Inside the braces is a piece , Only valid in the current block ;

  • let The defined variables can be modified , But it cannot be defined more than once in the same scope ,
      let v1 = 1;
      v1 = 2; /* Change the value of the variable */
      console.log(v1); /*2, Can be modified within a scope */
      let v1 = 3;
      console.log(v1); /* Report errors , A scope cannot be declared repeatedly */

The same variables can be defined in different scopes  , Mutual interference . Because they are in different scopes .

      let v1 = 1;
      console.log(v1);  /*1*/


      if (v1 === 1) {
        let v1 = 2;
        console.log(v1); /*2*/
      }
  •  let The variable of .let The declaration of is also promoted to the top of the scope , But it will not undefined assignment . If it is used without assignment, an error will be reported
          function func() {
            console.log(name); /* Not stated in advance name, Report errors */
            let name = 'fkit';
            console.log(name);
          }
           func();

    3. const

  • const It's a block level scope  
  • const Cannot be modified and cannot be redeclared , It is equivalent to defining a constant .
      const v4 = 1;
      v4 = 3;/* Attempt to modify value */
      console.log(v4);/* Report errors */
      const v4 = 1;
      const v4 = 2;
      console.log(v4); /* Report errors */
  • All say const The defined value cannot be modified , In fact, the address of the defined value cannot be modified . Because when const When complex data types are defined , You can also manipulate his elements .

        When using const When defining objects , You can use object names . attribute   = Modify the attribute value of an object by attribute value .

const obj = {
    times: 4,
};
obj.times = 5;
  •  const The variable of , And let equally , Promote to the top of the scope , But not initialized . If the definition is used without assignment, an error will be reported

原网站

版权声明
本文为[tingkeaiii]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270542515166.html