1. Differences in scope:
{ let a = 10; var b = 1; } a // ReferenceError: a is not defined. b // 1
var
The keyword is a global variable or a function variable
let
The keyword is a block scope
##2. The difference between defining and using variables:
// var 的情况 console.log(foo); // 输出undefined var foo = 2; // let 的情况 console.log(bar); // 报错ReferenceError let bar = 2;
let must be defined first and then used,
var can be used first and then declared. When using an undefined variable, its value is undefined
##3 .Differences in repeated declarations: The repeated declaration of keywords is correct; Repeat keyword declaration error; Uncaught SyntaxError: Identifier 'b' has already been declared The above is the detailed content of The difference between let and var in JS. For more information, please follow other related articles on the PHP Chinese website! var a;
var a;
let b;
let b;
2021 js interview questions and answers ( Large summary)