Scope
Local Variable and Global Variable
  • Local variables are deleted when the function is completed
  • Global variables are deleted when you close the browser window or tab
  • var a = 10; // global variable
    
    function f()
    {
    	var b = 100; // local variable b
    	console.log(a); // access global variable a
    }
    
    f();
    
    // console.log(b); // cannot access the local variable defined in f()
    		
    Define Global Variables
  • All global variables belong to the window object
  • Do NOT create global variables unless you intend to, your global variables or functions can overwrite window variables or functions
  • a = 10; // undeclared variable is referenced, a new variable gets created in the global object
    
    // a and window.a are the same object
    console.log(a) // 10
    console.log(window.a) // 10, global object window
    
    var c = 1;
    
    console.log(c); // 1
    console.log(window.c); // 1
    
    function f()
    {
    	b = 100;
    }
    
    f();
    
    console.log(b); // 100
    console.log(window.b); // 100, b is not removed after calling function f
    		
    Variables in Google Chrome Console
    for(var b in window) { 
      	if(window.hasOwnProperty(b)) console.log(b); 
    }
    		
    Function Arguments
    function f(a)
    {
    	a++;
    	console.log(a);
    }
    
    // console.log(a); // not defined
    		
    Block Scope
    {
    	var a = 100; // global scope
    }
    
    console.log(a);
    console.log(window.a);
    
    {
    	let b = 100; // block scope
    }
    
    // console.log(b); // b is undefined
    		
    var a = 10;
    
    {
    	var a = 100; // overwrite a outside block
    	console.log(a); // 100
    }
    
    console.log(a); // 100
    
    var b = 10;
    
    {
    	let b = 100; // screen b outside block
    	console.log(b); // 100
    }
    
    console.log(b); // 10
    		
    let b = 10; // b is not a attribute of window object
    
    console.log(b); // 10
    console.log(window.b); // undefined
    		
    Loop Scope
    var i = 5;
    for (var i = 0; i < 10; i++) {
      	// some statements
    }
    		
    console.log(i); // Here i is 10
    
    let j = 5;
    for (let j = 0; j < 10; j++) {
      	// some statements
    }
    		
    console.log(j); // Here j is 5
    		
    Const
  • Const behaves like let, except they cannot be reassigned
  • Const defines a constant reference to a value, cannot change constant primitive values, but we can change the properties of constant objects
  • const PI = 3.141592653589793;
    //PI = 3.14;      // This will give an error
    //PI = PI + 10;   // This will also give an error
    
    // block scope
    var a = 10;
    
    {
    	const a = 100;
    }
    
    console.log(a); // 10
    
    const person = {name: 'Lin', age: 39};
    
    //person_1 = {}; // not allowed
    
    person.name = 'Yanhua';
    
    console.log(person); // can change the properties
    
    const cars = ['Buick', 'Acura', 'Lincoln', 'Honda'];
    
    // cars = []; // not allowed
    
    cars[3] = 'Toyota';
    cars.push('Audi');
    
    console.log(cars); // constant array can change
    		
    Nested Functions
  • All functions have access to the scope "above" them
  • function add() {
      var counter = 0;
      function plus() {counter += 1;}
      plus();    
      return counter; 
    }
    		
    Reference
  • Memory Management