When reading older JavaScript code, you will often see variables declared with var instead of let or const. var was the original way to create variables in JavaScript, but it has some quirks that can cause tricky bugs.
First quirk: var ignores { } blocks! A variable declared with var inside an if or a for loop leaks right out of the block and remains accessible afterward:
↗️
Loading editor...
↶
↷
Unlike let and const, var only respects function boundaries, never { } blocks. That means a loop counter declared with for (var i = 0; ...) stays alive even after the loop is finished: