Variable Declaration with var
When a variable is declared using var, it is added to the current context.
function add() {
var color = 'blue';
}
document.writeln(color);//error
Without var keyword variable is globle scoped.
function add() {
color = 'blue';
}
document.writeln(color);//blue
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Variables:
- Primitive and reference values
- Determining Type: typeof vs instanceof
- Execution Context And Scope
- No Block-Level Scopes
- Variable Declaration with var