Skip to content

Instantly share code, notes, and snippets.

@mikebranski
Created February 17, 2017 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebranski/be727f5bd34aebc837ef88e673be0be0 to your computer and use it in GitHub Desktop.
Save mikebranski/be727f5bd34aebc837ef88e673be0be0 to your computer and use it in GitHub Desktop.
Bloc JS Closures / Scoping exercises

JS Closures / Scoping exercises

  1. What’s the result of executing this code and why.
function test() {
   console.log(a);
   console.log(foo());
   
   var a = 1;
   function foo() {
      return 2;
   }
}

test();
  1. What is result?
var a = 1; 

function someFunction(number) {
  function otherFunction(input) {
    return a;
  }
  
  a = 5;
  
  return otherFunction;
}

var firstResult = someFunction(9);
var result = firstResult(2);
  1. What is the result of the following code? Explain your answer.
var fullname = 'John Doe';
var obj = {
   fullname: 'Colin Ihrig',
   prop: {
      fullname: 'Aurelio De Rosa',
      getFullname: function() {
         return this.fullname;
      }
   }
};

console.log(obj.prop.getFullname());

var test = obj.prop.getFullname;

console.log(test());
  1. What will you see in the console for the following example?
var a = 1; 
function b() { 
    a = 10; 
    return; 
    function a() {} 
} 
b(); 
console.log(a);    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment