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  

Variables:
  1. Primitive and reference values
  2. Determining Type: typeof vs instanceof
  3. Execution Context And Scope
  4. No Block-Level Scopes
  5. Variable Declaration with var