Earlier quoted context omitted.
Honest question here --- What is the difference between let and global variables? There are hundreds of articles written about the doom associated with PHP globals, but let appears to be universally lauded. I must be missing something, but I can't tell where.
It's spelled out pretty well in the article. But if you want another example, consider these two code blocks: var foo; var bar; { let foo = "hello"; var bar = "world"; } console.log(foo); console.log(bar); This produces: undefined world The reason being that the `let` statement restricted that variable to the block it was in (defined by the { and }). `var` declares the variable globally, allowing it to be accessed ou…
Not quite. var's are hoisted to the top of their most local function.
(function(){
(function(){
var x = 123;
})();
{
var y = 123;
}
// Here, x is not defined, but y is
})();
// Here, neither x nor y are defined
The above code essentially gets translated to the following: (function(){
var y;
(function(){
var x;
x = 123;
})();
{
y = 123;
}
// Here, x is not defined, but y is
})();
// Here, neither x nor y are defined