Skip to content

Latest commit

 

History

History
391 lines (277 loc) · 3.74 KB

File metadata and controls

391 lines (277 loc) · 3.74 KB

Hoisting

  1. What will be the output ?

    console.log(typeof foo);
    
    function foo() {
      return "bar";
    }
    var foo = "bar";
    • A: undefined
    • B: string
    • C: function

    Answer

    Option: C


  2. What will be the output ?

    function foo() {
      return "bar";
    }
    var foo;
    
    console.log(typeof foo);
    • A: undefined
    • B: string
    • C: function

    Answer

    Option: C


  3. What will be the output ?

    if (true) {
      function foo() {
        console.log(1);
      }
    } else {
      function foo() {
        console.log(2);
      }
    }
    
    foo();
    • A: 1
    • B: 2

    Answer

    Option: A


  4. What will be the output ?

    function foo() {
      bar();
    
      return;
    
      function bar() {
        console.log("bar");
      }
    }
    
    foo();
    • A: undefined
    • B: bar
    • C: Uncaught ReferenceError: bar is not defined

    Answer

    Option: B


  5. What will be the output ?

    function foo(x) {
      x();
    
      function x() {
        console.log("foo");
      }
    }
    
    foo(function() {
      console.log("bar")
    });
    • A: foo
    • B: bar
    • C: Uncaught ReferenceError: x is not defined

    Answer

    Option: A


  6. What will be the output ?

    foo();
    
    function foo() {
      console.log(1);
    }
    
    var foo = function() {
      console.log(2);
    };
    
    function foo() {
      console.log(3);
    }
    
    foo();
    • A: 3 3
    • B: 3 2
    • C: 1 2
    • D: 1 3

    Answer

    Option: B


  7. What will be the output ?

    function animal(){
      console.log("Cat");
    }
    
    var otherAnimal;
    
    animal();
    otherAnimal();
    
    otherAnimal = function() {
      console.log("Dog");
    }
    • A: Cat Dog
    • B: Cat undefined
    • C: Cat TypeError: otherAnimal is not a function

    Answer

    Option: C

  8. What are the logged values of a and b ?

    b = function a(){};
    var a = b = 6;
    a = function b(){};
    function b() {};
    function a() {};
    console.log(a,b);
    • A: ƒ b(){} 6
    • B: ƒ a(){} 6
    • C: ƒ b(){} ƒ a(){}
    • D: ƒ a(){} ƒ b(){}
    • E: 6 ƒ a(){}
    • F: 6 ƒ b(){}

    Answer

    Option: A

  9. What are the logged values of a and b ?

    var a = 10;
    console.log("line number 2", a);
    
    function fn() {
      console.log("line number 4", a);
      var a = 20;
      a++;
      console.log("line number 7", a);
      if (a) {
        var a = 30;
        a++;
        console.log("line number 11", a);
      }
      console.log("line number 13", a);
    }
    fn();
    console.log("line number 2", a);
    • A:
    line number 2 10
    line number 4 10
    line number 7 21
    line number 7 31
    line number 13 31
    line number 2 31
    • B:
    line number 2 10
    line number 4 undefined
    line number 7 21
    line number 11 31
    line number 13 31
    line number 2 10
    • C:
    line number 2 10
    line number 4 10
    line number 7 21
    line number 11 31
    line number 13 31
    line number 2 10

    Answer

    Option: B


  10. What will be the output?

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

    Answer

    Option: B