Exercise – Scope

by iamKrickE

JavaScript

/**
 * Exercise:
 *
 * Function Scope
 *
 * Work in the console, or use "console.log()" to output to the console 
 * from the script
 *
 * Write two functions that share the same, global variable
 * 	- Verify this by logging the value of the variable to the console 
 *    from within each function
 * Write two functions that share a non-global variable
 *  - Verify this by logging the value of the variable to the console 
 *    from within each function
 */

var derp = 'abc';
function herpa(){
    derp += 'def';
    derpa();
}
function derpa () {
    derp += 'ghi';
}
herpa()
console.log(derp);






function sharedDerp (){    
    var x = 'xyz';
    sharedHerp();
    function sharedHerp(){
        x += 'www';
    }
    return x;
}

console.log(sharedDerp());