Scopes Flashcards

1
Q
What will the below code print out?
"use strict";
var name = 'igloo';
var code = "var name = 'asim';"
eval(code)
console.log(name)
A

igloo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
// What will this code print?
    (function(){
      var a = 3;
    })();
    console.log("a defined? " + (typeof a !== 'undefined'));
A

a defined false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
// What will this code print?
    console.log(moo);
    var moo = function() {
        console.log("loo");
    };
A

undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
for (var i = 0; i < 5; i++) {
  var btn = document.createElement('button');
  btn.appendChild(document.createTextNode('Button ' + i));
  btn.addEventListener('click', function(){ console.log(i); });
  document.body.appendChild(btn);
}
// What gets logged to the console when the user clicks on “Button 4”?
A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

console.log((function f(n){return ((n > 1) ? n * f(n-1) : n)})(4));

A

This is an IIFE that actually performs a factorial, so 4! which is 24

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
// Consider the code snippet below. What will the console output be?
(function(x) {
    return (function(y) {
        console.log(x);
    })(2)
})(1);
A

Javascript searches the functions outer scope and in there it will find x and the value of x is 1 since that is what was passed in through the IIFE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
// Consider the code snippet below. What will the console output?
function loo() {
  var goo = 1;
  moo();
}
function moo() {
  console.log(goo);
}
loo();
A

The goo variable isn’t visible on the moo function due to scope lookup rules.

uncaught REferenceError: goo not defined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

// What will be the output of first console.log in the code below?
var salary = “1000$”;
(function () {
console.log(“Original salary was “ + salary);
var salary = “5000$”;
console.log(“My New Salary “ + salary);
})();

A

The var salary in the IIFE is hoisted to the top of the function scope, so that means that salary is set to undefined by the time the first console.log is run

Original Salary was undeffined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
// What happens when you execute the code below
    a = 1;
A

It creates a global variable called “a”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
// What does this print out?
    console.log(typeof(null));
A

Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can closures refer to variables inside functions (outer scopes)

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can closures refer to variables even after the function has been returned?

A

Yes (only current value of variables)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
//What does this return 
var foo = [];
for(var i =0; i < 10; i++){
    foo[i] = function() {return i};
}

console. log(foo0);
console. log(foo1);
console. log(foo2);

A

10
10
10

Why? Because the closure points to the actual value of i in the outer scope (after the for loop has finished)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
//How to get 1 -3 output from this
var foo = [];
for(var i =0; i < 10; i++){
    foo[i] = function() {return i};
}

console. log(foo0);
console. log(foo1);
console. log(foo2);

A
var foo = [];
for(var i =0; i < 10; i++){
 // use a intimidate invoked function //
    (function(){
        var y = i;
        foo[i] = function() {return y};
    })();        
}
console.log(foo[0]());
console.log(foo[1]());
console.log(foo[2]());
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
// improve //
var foo = [];
for(var i =0; i < 10; i++){
 // use a intimidate invoked function //
    (function(){
        var y = i;
        foo[i] = function() {return y};
    })();        
}
A
var foo = [];
for(var i =0; i < 10; i++){
    (function(y){
        foo[y] = function() {return y};
    })(i);     
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly