Javascript interview questions

Javascript quiz questions

  • 1.

    What does the following code print to the console?

    console.log(function () {
      return "Hey hey hey";
    }());

    Answer:

    "Hey hey hey"
    

    Notice that the function is defined and invoked in a single statement.

    View
  • 2.

    Demonstrate that the this keyword refers to the object that is being created for functions that are invoked in the constructor invocation context.

    Answer:

    function Paper() {
      this.whatever = "ok";  // "this" refers to the object that is created by the Paper() function
    }
    var my_paper = new Paper();
    console.log(my_paper.whatever);  // The object created by the Paper() constructor function has a whatever property.  In the body of the constructor function, the "this" keyword refers to the object that is being created and referring to "this" allows properties to be added to the object that is being constructed.
    View
  • 3.

    Demonstrate that the this keyword refers to the object itself for functions that are invoked in the method invocation context.

    Answer:

    var cup = {
      something: function() {
        return this === cup;
      }
    }
    console.log(cup.something());  // prints "true" to the console
    View
  • 4.

    Demonstrate that the this keyword equals the global object for functions that are invoked with function invocation context.

    Answer:

    function exampleFunction() {
      if (this === window) {
        return "this equals the global object with function invocation context";
      }
    }
    console.log(exampleFunction());
    

    Functions that are not properties of an object and are not constructor functions are invoked in the function invocation context.

    View
  • 5.

    Explain what the following code prints to the console.

    function pirate () {
      return this;
    }
    console.log(pirate());

    Answer:

    The pirate() function returns the global window object. The global window object is created by the JavaScript interpreter when it is started to store built-in constructor functions, global constants, and global properties. When functions are invoked as functions (i.e. they are in the global namespace and are not part of an object or constructor function), the value of the this keyword is the global object.

    View
  • 6.

    Create a Circle() constructor function to make circle objects with an area method. Add a private PI variable to the circle objects that are created a set its value equal to 3.14.

     

    Answer:

    function Circle(radius) {
      var PI = 3.14;
      this.radius = radius;
      this.area = function () { return PI * Math.pow(this.radius, 2) };
    }
    
    var my_circle = new Circle(5);
    console.log(my_circle.area());
    View
  • 7.

    Create an Employee() constructor function with days_worked, vacation_days_per_year, and vacation_days_taken properties. Add a vacation_days_left() method that returns the number of vacation days the employee has remaining.

    Answer:

    function Employee (days_worked, vacation_days_per_year, vacation_days_taken) {
      this.days_worked = days_worked;
      this.vacation_days_per_year = vacation_days_per_year;
      this.vacation_days_taken = vacation_days_taken;
      this.vacation_days_left = function () {
        return(this.days_worked / 365 * vacation_days_per_year - vacation_days_taken);
      }
    }
    
    var sally = new Employee(180, 20, 4);
    console.log(sally.vacation_days_left());
    View
  • 8.

    Create a constructor function called Square() to create square objects with a side property and a method to calculate the area. Use the Square() function to create a square object.

     

    Answer:

    function Square(side) {
      this.side = side;
      this.area = function () { return this.side * this.side };
    }
    
    var my_square = new Square(4);  // creates a square object
    my_square.area();  // returns 16
    my_square.side;  // returns 4
    View
  • 9.

    What does the following example print to the console?

    var globalObject = window;
    function nestedExample() {
      function innerFunction () {
        return this === globalObject;
      }
      return innerFunction();
    }
    console.log(nestedExample());

    Answer:

    true
    

    Due to a design flaw in the language, inner functions use the function invocation context, not the method invocation context, so the value of "this" in the innerFunction() equals the globalObject. Additional quirks and complicated features of the JavaScript language will be covered in subsequent quizzes.

    View
  • 10.

    What does the following example print to the console?

    console.log((function (x, y) { return x + y })(3, 4));

    Answer:

    7
    

    The anonymous function in this example is defined and invoked on the same line. JavaScript functions are frequently invoked immediately after they are defined, so be prepared to see this technique used frequently.

    View
  • 11.

    What does the following code print to the console? Explain the use of the "this" keyword.

    function Whiteboard() {
      this.purpose = "brainstorm";
      this.businessReason = function () {
        return this.purpose + "…..profit!!"
      }
    }
    var w = new Whiteboard();
    console.log(w.businessReason());

     

    Answer:

    brainstorm…..profit!!
    

    The businessReason() method uses the "this" keyword to refer to the object that is being created by the constructor function and is assigned to the variable w in this example.

    View
  • 12.

    What does the following code print to the console?

    function Raisin() {
      this.formerSelf = "grape";
      this.goodness = function (number) {
        return "I am " + number + " times worse than when I was a " + this.formerSelf
      };
    }
    var r = new Raisin();
    console.log(r.goodness(10));

    Answer:

    "I am 10 times worse than when I was a grape"
    

    When functions are invoked as constructors, the "this" keyword refers to the object that is being created. In this example, we are creating an object that is assigned to the r variable. The "this" keyword in the goodness() function refers to the object that is assigned to the r variable.

    View
  • 13.

    What does the following code print to the console? Explain the use of the "this" keyword.

    var honeyBadger = {
      personality: "vicious",
      modusOperandi: function () {
        return "I am " + this.personality + " because I don't care!";
      }
    }
    console.log(honeyBadger.modusOperandi());

    Answer:

    "I am vicious because I don't care!"
    

    Notice that the "this" keyword in the modusOperandi() method refers to the honeyBadger object. When functions are invoked as methods, "this" refers to the object that defines the method.

    View
  • 14.

    What does the following code print to the console?

    var an_object = {
      hat: function() { return (this === an_object); }
    }
    console.log(an_object.hat());

    Answer:

    true
    

    The hat() method is bound to an_object and the this keyword within the hat() method refers to an_object.

    View
  • 15.

    What does the following code print to the console?

    function sampleFun() {
      console.log(this);
    }

    Answer:

    The global window object

    The this keyword has different values depending on when it is used. When the this keyword is used in a function that is invoked in a function context (meaning the function is not part of a constructor function or the property of an object), then the this keyword refers to the window object. The this keyword is rarely used for functions that are invoked in a function context.

    View
  • 16.

    What is the difference between the whatever() and best_quote() functions in the following code?

    function whatever() {
      return "talk to the hand";
    }
    
    var clueless = {
      best_quote: function() { return "That’s Ren and Stimpy. They’re way existential." }
    }

    Answer:

    whatever() is a function in the global namespace. best_quote() is a property of the clueless object, so it is a special type of function called a method. When whatever() and best_quote() are invoked, they are said to have different invocation contexts because they are called in different ways. whatever() is invoked as a function and best_quote() is invoked as a method.

    View
  • 17.

    Invoke the zombies function.

    function zombies() {
      return "We like to eat people";
    }

    Answer:

    zombies();
    

    Invoking a function is just a fancy way to say "running a function" or "executing a function". JavaScript functions are invoked by adding () to the end of the function name.

    View
  • 18.

    What does the following line return when typed into the JavaScript web console of an internet browser?

    this === window

    Answer:

    true
    

    window is a keyword that refers to the global object. this refers to different objects in different contexts, but it refers to the window object in the top level namespace.

    View
  • 19.

    Type the "this" keyword into the JavaScript web console of your favorite browser (hopefully Chrome or Firefox) and explain what is returned.

    Answer:

    this  // the window object is returned
    

    The global object is created when the JavaScript interpreter starts and it creates global properties (i.e. NaN), global functions (i.e. parseInt()), built-in constructor functions (Array()), and global objects (Math). The global object for client-side JavaScript is called window.

    View
  • 20.

    The prototype property of constructor functions can also be set to add methods to objects created by constructor functions. JavaScript functions are objects and the prototype property can be used to add methods to the objects that are created by constructor functions.

    function Dog(name) {
      this.name = name;
    }
    
    Dog.prototype = {
      constructor: Dog,
      bark: function () { return "ruff ruff" }
    }
    
    var rover = new Dog("red rover");
    console.log(rover.name);  // returns "red rover"
    What does the following line print to the console?
    console.log(rover.bark());

    Answer:

    "ruff ruff"
    

    The prototype design pattern is used to JavaScript to give methods to objects created by constructor functions.

    View

© 2017 QuizBucket.org