The interviewer actually asked me in JavaScript var, let const and what difference such a simple question?

这几天逛CSDN论坛,发现在一些前端模块发的问题大都是因为var、let和const搞不清楚导致的,今天我来浅析下,希望对看到这篇博客的童鞋有所帮助~


What we know is that ... (English Composition universal beginning) : There are three variables declared in JavaScript the way: var, let, const.

table of Contents

1. Be

(1) After the variable var defined can be modified, if initialization is not output undefined, no error.

var a;
console.log(a);   // undefined
// -----------------------------
console.log(a);     // undefined
var a = 1;

(2) var defined variable, across the block access can not access across function.

(3) var only function scope, not block-level scope.

(4) var is scoped scope of the function, can be used to declare a global variable var, may declare a local variable. In a 函数内a variable declared using var, then the variable is effective in this function.

  • Global Variables: variables defined outside a function, the entire scope of the code file.
  • Local variables: variables defined in the function, the current scope is internal function.
var a = 666;  
console.log("函数外var定义a:" + a); // 函数外var定义a:666

function cg(){
a = 888;
console.log("函数内var定义a:" + a);// 函数内var定义a:888
}

cg();
console.log("函数调用后var定义a为函数内部修改值:" + a);
// 函数调用后var定义a为函数内部修改值:888

(5) can be defined repeatedly overwrite the previous value of the latter.

var a = 1;
var a = 2;
console.log(a); // 2

2. let

(. 1) is a block-level scope let, let the internal function definitions used, no effect on the external function.

let a = 666;  
console.log("函数外let 定义a:" + a); // 函数外let 定义a:666

function cg(){
let a = 888;
console.log("函数内let 定义a:" + a);// 函数内let 定义a:888
}

cg();
console.log("函数调用后let 定义a为函数内部修改值:" + a);
// 函数调用后let定义a为函数内部修改值:666

(2) there is no variable declarations in advance, otherwise it will error.

console.log(a);
let a = 1;

Here Insert Picture Description

(3) let-defined variables that can only be accessed in block scope, can not cross block access, it can not cross function access.

    var c=11;
{
    let c=12;
    console.log(c);// 12
}
    console.log(c);// 11

(4) can not be redefined, otherwise it will error.

let a = 1;
let a = 2;
console.log(a);

Here Insert Picture Description

3. const

(1) const variable defined 不可以modifications, and 必须initialization.

const b = 2;// 正确
const b;   // 错误,必须初始化

(2) const generally used to declare a constant, and a constant declaration is not allowed to change, it is 只读属性therefore necessary in the 声明的同时赋值.

(3) const and let, are block-level scope, can only be accessed block scope, the temporary presence of the dead, there is no variable declarations in advance, does not allow duplicate definitions.

const b = 2; 
const b = 2; 
console.log(b);

Here Insert Picture Description


Finally, we can use 闭包to prevent global pollution, see: Closure Principle

Published 88 original articles · won praise 398 · views 70000 +

Guess you like

Origin blog.csdn.net/weixin_42881768/article/details/105056122