关于javascript:let变量等于var变量吗?

Is a let variable equal to a var variable?

本问题已经有最佳答案,请猛点这里访问。

如果我有类似的东西:

1
2
let x = 20;
var z = 20;

1
x === z


===不比较变量-它比较值。考虑到两个变量的值相同(20),它们将"相等"。

这些变量如何声明并不重要,只是它们都在范围内,并且在评估===操作时分配了该值。在示例片段中就是这样。


试试看你自己…

(如果没有显示任何内容,这是因为您使用的浏览器不支持let。)

1
2
3
4
5
6
"use strict";

let x = 20;
var z = 20;

document.write(x === z);

请阅读此答案,了解有关letvar之间差异的详细信息:

The difference is scoping. var is scoped to the nearest function block (or global if outside a function block), and let is scoped to the nearest enclosing block (or global if outside any block), which can be smaller than a function block.