Today, when I encounter variables defined by const, I summarize the differences between const, var, and let.
1. Variables defined by const cannot be modified and must be initialized.
const b =4;//正确
const b;//错误,必须初始化
console.log('函数外const定义b:' + b);//有输出值
console.log('函数外修改const定义b:' + b);//无法输出
2. The variable defined by var can be modified. If it is not initialized, it will output undefined and no error will be reported.
3. Let is a block-level scope. After the function is defined with let, it has no effect on the outside of the function.
let c = 2;
console.log('函数外let定义c:' + c);//输出c=2
function change(){
let c = 3;
console.log('函数内let定义c:' + c);//输出c=3
}
change();
console.log('函数调用后let定义c不受函数内部定义影响:' + c);//输出c=2