Skip to content

Latest commit

 

History

History
209 lines (139 loc) · 2 KB

scopes&closures.md

File metadata and controls

209 lines (139 loc) · 2 KB

Scopes & Closures

  1. Which variables end up being part of func's closure ?

    var c = 10;
    
    function foo(a){
        let b = 8;
        const d = 10;
        return function bar(){
            return a + d + c;
        }
    }
    
    const func = foo(7);
    • A: a and d
    • B: a,b and d
    • C: a,b,c and d
    • D: a,d and c

    Answer

    Option: A


  2. What will be the output ?

    const fn = () => {
      a = 2;
      console.log(a);
    }
    
    fn();
    
    • A: undefined
    • B: 2
    • C: Uncaught ReferenceError: a is not defined

    Answer

    Option: B


  3. What will be the output ?

    for (var i = 0; i < 4; i++) {
      setTimeout(() => console.log(i), 0)
    }
    • A: 0 1 2 3
    • B: 4 4 4 4

    Answer

    Option: B


  4. What will be the output?

    for(let i = 0; i < 5; i++){
        setTimeout(() => console.log(i), 1);
    }
    • A: 0 0 0 0 0
    • B: 1 2 3 4 5
    • C: 5 5 5 5 5
    • D: 0 1 2 3 4

    Answer

    Option: D


  5. What will be the output?

    let count = 0;
    (function immediate() {
      if (count === 0) {
        let count = 1;
        console.log(count);
      }
      console.log(count);
    })();
    • A: 0 0
    • B: 1 1
    • C: 1 0
    • D: 0 1

    Answer

    Option: C


  6. what would be the typeof a and typeof b

    function foo() {
      let a = b = 0;
      a++;
      return a;
    }
    foo();
    console.log(typeof a)
    console.log(typeof b)
    • A: Number Number
    • B: Number undefined
    • C: undefined Number
    • D: not define not define

    Answer

    Option: C