The var
statement is used to declare a variable of global scope.
var variableName;
The var
keyword is associated with a name. The name is attached to the right side of the var
keyword, the name becomes a variable name.
if(true) {
var hello = "Hello";
}
console.log(hello); // Prints: Hello
The var declarations can be declared multiple times. The duplicate declarations will not cause an issue or trigger an error. Variable declared from inside a block also becomes a global variable. This is not used because of this behavior as let and const do a better job of making block-scoped variables.