Understanding var, let and const with Examples

Var

Using var declares either a locally(within function) scoped variable or a globally scoped variable.

When var is used outside a function, it declares a globally scoped variable.

var a = 2; // globally scoped variablefunction foo() {
console.log(a);
}
foo(); // prints out 2console.log(window.a); // prints out 2