Before ES6, variables were defined with var, which could be assigned to the global object unless strict mode was used, whereas ES6 introduces let and const alongside var for defining variables.
var
var |
Using var
outside a function places the variable in the global scope. Inside a function, var
is scoped to that function, but when used inside a block (such as a for
loop), the variable is still accessible outside of the block.
var has function scope rather than block scope. |
let
let |
let is block-scoped and restricted to the block or expression where it is defined, making it available only within that block, such as inside a for loop.
let has block scope. |