Skip to content

Instantly share code, notes, and snippets.

@thatPamIAm
Created May 7, 2019 14:26
Show Gist options
  • Save thatPamIAm/3058508c07a1ffc8ab1fffae445f9dfd to your computer and use it in GitHub Desktop.
Save thatPamIAm/3058508c07a1ffc8ab1fffae445f9dfd to your computer and use it in GitHub Desktop.

Scope

What is the result of each console.log()? Explain your answer.

PROBLEM 1

var alarmTime = 8;

function testAlarm() {
   let alarmTime = 22;
   if (alarmTime > 10) {
     console.log('A', alarmTime); 
     console.log('Wake up!');
   }
}

console.log('B', alarmTime);

for (let i=0; i<5; i++) {
   alarmTime++;
}

console.log('C', alarmTime);

testAlarm();

console.log('D', alarmTime);

PROBLEM II

let number = 10;

function foo () {
  let number = 20;
  console.log(number);  // 1 - what will log here?
}

console.log(number);    // 2 - what will log here?

foo()

console.log(number);    // 3 - what will log here?

PROBLEM III

let array = [];

for (var i = 0; i < 10; i++) {
  array[i] = function () {
    console.log('You clicked: ' + i);
  };
}

array[5]()  // what will log here?

PROBLEM IV

let array = [];

for (let i = 0; i < 10; i++) {
  array[i] = function () {
    console.log('You clicked: ' + i);
  };
}

array[5]()  // what will log here?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment