Javascript interview questions

Javascript quiz questions

  • 1.

    What does the following function print to the console?

    function sum(a, b) {
      return a + b;
    }
    console.log(sum(1, 2, 3, 4));

    Answer:

    3
    

    When too many arguments are supplied to a function they are simply ignored. The sum() function only takes 2 arguments so the extra arguments (3 and 4) are ignored in this example.

    View
  • 2.

    What does the following code print to the console?

    function foo () {
      return bar();
      function bar() {
        return "Poppin' bottles";
      }
    }
    console.log(foo());

    Answer:

    "Poppin' bottles"
    

    The bar() method is hoisted to the top of the foo() method, so it can be called before it's defined.

    View
  • 3.

    What does the following code print to the console?

    blah = function () {
      return lala;
      lala = "hi";
    };
    console.log(blah());

     

    Answer:

    This raises a ReferenceError because the lala variable cannot be returned before it's defined.

    View
  • 4.

    What does the following code print to the console?

    var cat = function(x) {
      return x * 2;
    }
    console.log(cat.name);

     

    Answer:

    ""
    

    The name property for anonymous functions returns an empty string.

    View
  • 5.

    What does the following code print to the console?

    function blabbermouth() { };
    console.log(blabbermouth.name);

     

    Answer:

    "blabbermouth"
    

    Functions have a name property that returns the name of a function. JavaScript functions are objects and can have properties like any other object.

    View

© 2017 QuizBucket.org