QUESTION:

True or False: the terms “scope” and “context” refer to the same thing in JavaScript.

Click for Answer

QUESTION:

If you omit the “var” keyword when creating a variable in a function, it becomes a property of what object?

Click for Answer

QUESTION:

How do you determine if a JavaScript instance object was created from a specific constructor or not?

Click for Answer

QUESTION:

True or False: Once you create an object, you can add, remove or change properties of that object at any time.

Click for Answer

QUESTION:

True or False: You can only instantiate a JavaScript constructor function once.

Click for Answer

QUESTION:

When you create a function inside of a method, what does the “this” keyword refer to when used in that function?

Click for Answer

QUESTION:

Name two ways two change the context of a JavaScript method

Click for Answer

QUESTION:

Can a JavaScript constructor return a primitive value (e.g. a number or a string)?

Click for Answer

QUESTION:

In JavaScript, are objects passed by reference or by value?

Click for Answer

QUESTION:

What is the difference between a constructor function and a non-constructor function with respect to the word “this”

Click for Answer

QUESTION:

What is the name of the property that allows you to add properties and methods to an object, as well as every object that inherits from it?

Click for Answer

QUESTION:

True or False: An object literal can be used to create private variables.

Click for Answer

QUESTION:

What is the name of the object that refers to the application used to view a web page?

Click for Answer

QUESTION:

True or False: The object returned by the document.getElementsByTagName() method is an array.

Click for Answer


QUESTION:

Which object.property combination provides a reference to the protocol used to view the current web page?

Click for Answer

QUESTION:

Given the below code, what line of JavaScript would allow foo.getColor to return the value of foo.color, without using the word “foo”?

Click for Answer (+)

QUESTION:

What is the difference between using dot notation and bracket notation when accessing an object’s property?

Click for Answer (+)

QUESTION:

What is important to remember about the last property’s value in a JavaScript object literal?

Click for Answer

QUESTION:

Given the following code, what is very likely the reason that the programmer made the first letter of “Foo” a capital letter?

Click for Answer

QUESTION:

Given the following code, how would you instantiate the function “Foo” and assign it to the variable “bar”?

Click for Answer

QUESTION:

What are two ways in which the variable “foo” can be assigned to an empty object?

Click for Answer

QUESTION:

True or False: When you create an object literal, you must first instantiate the Object() constructor.

Click for Answer

QUESTION:

When using the addEventListener() method to create a click-handler for a DOM element, what is the value of “this” inside of the callback you specify?.

Click for Answer

QUESTION:

True or False: A JavaScript array is not an object

Click for Answer

QUESTION:

If a string is a primitive value, why does it have a split() method?

Click for Answer

QUESTION:

True or False: A function can have its own properties and methods.

Click for Answer

19 Comments

  • hungnh1704

    hi kevin,
    about the question :
    QUESTION:
    In JavaScript, are objects passed by reference or by value?
    And your answer is all objects are passed by reference.

    But in my opinion, everything is pass by value, but in case of object, the “value” which is passed here is “reference”.
    That’s just my own knowledge, I think that there are no “pass by reference”, just only pass by value and depending on what type of data is, what the value pass is ( value or its address, what we called reference).
    Sorry if it’s wrong.

    • Derek Pavao

      I tend to think about the pass by reference vs by value stuff in this manner. An objects properties are passed by reference, but the object it self is still passed by value. If you assign the variable passed to a function to a different object, that assignment is not propagated to the outer scope.

      var testObj = {orignal: ‘object’};

      var testReference = function (obj) {
      obj = {new: ‘object’};
      };

      testReference(testObj);
      console.log(‘testObj’, testObj);

      testObj here is still the original object. Does this make sense, or do i have my terminology wrong?

      • Kevin Chisholm Kevin Chisholm

        Hi Derek,

        Thanks for y our comment. It’s an interesting point and really great example that demonstrates it.

        The way I look at your example is: The “obj” parameter is a private variable. When you assign a new object to it, you wipe out the reference to the passed-in object. There is no longer any connection between obj and testObj.

        Here is an example of what I mean:

        var testObj = {orignal: ‘object’};

        var testReference = function (obj) {
        //obj = {new: ‘object’};

        obj.newProperty = true;

        console.dir(obj);
        };

        testReference(testObj);

        console.dir(testObj);

        You should see the following in your console:

        orignal “object”
        newProperty true

        (the above output appears twice)

        So, the changes you make inside the function to the passed-in object’s property do in-fact persist outside of the function. The object is passed by reference (as per my example). But when you overwrite that passed-in reference (as per your example), you essentially have a new object.

        Let me know if you disagree.

        Best Wishes,

        Kevin

        • Derek Pavao

          Hey Kevin,

          I think everything you said makes perfect sense. I think the confusion for a lot of beginners is that if the object is passed by reference, they think re-assigning the variable should persist outside the function. JavaScript doesn’t work this way because of its scoping. However, languages like PHP do it this way. Which makes it confusing.

          Warning! PHP example:
          “obj”);
          testReference($test);
          print_r($test);
          ?>

          This prints the array(1,2,3) and in PHP land this object is said to have been passed by reference.

          • Derek Pavao

            Sorry, example didn’t print out correctly

            function testReference(&$obj) {
            $obj = array(1,2,3);
            }

            $test = array(“test” => “obj”);
            testReference($test);
            print_r($test);

          • Kevin Chisholm Kevin Chisholm

            Hi Derek,

            Thanks for y our follow-up. I can definitely understand why JavaScript would be so confusing in a situation like this, especially coming from PHP. I am not as experienced in PHP, but I do find myself scratching my head sometimes when it comes top reference/value in PHP, because I am “thinking JavaScript!” : – )

  • Shawn Vega

    Hi Kevin- Thank you very much for the detailed explanations/examples and answers! This has been very helpful.

    • Kevin Chisholm Kevin Chisholm

      Hi Shawn,

      Thanks for your message, I’m very happy if these question were helpful at all. Please let me know if there are any additional areas / topics that you feel would be helpful to cover.

      Kevin

  • vinod

    It’s a great help. Thanks for your effort.

    I need to understand one item from the above list.

    Can a JavaScript constructor return a primitive value (e.g. a number or a string)? The answer was ‘No’.. if i have a custom constructor function, it can return anything.. can u plz explain it further..

    • Kevin Chisholm Kevin Chisholm

      Hi Vinod,

      Thanks for your comment.

      Every function in JavaScript returns a value. You can return any value from a function. For example, this function returns the string “hello”:

      function foo(){
      return “hello”;
      }

      I could have returned the number 5, the boolean “false” or an array. You can return ANY valid JavaScript value from a JS function. If you do NOT explicitly return a value. the function returns: “undefined”.

      Constructors behave differently. they can only return an object. If you do NOT explicitly return a value from a constructor, the new instance of the constructor is returned. That new instance is an object.

      If you want to explicitly return a value from a constructor, that value MUST be an object. If you attempt to return a primitive value (e.g. a string or a number), the instance object is returned instead.

      So, for example:

      1) in this case, I try to return a string, but the instance object will be returned instead (returning a primitive is not allowed):

      function Foo(){
      this.color = “red”;

      return “hello”;
      }

      console.dir(new Foo()) // object -> color : red

      2) In this case, I return an array, and it works because it is an object:

      function Foo(){
      this.color = “red”;

      return [‘a’, ‘b’, ‘c’];
      }

      console.dir(new Foo()) // object -> 0 : ‘a’, 1 : ‘b’, 2: ‘c’

      3) In this case, I do not explicitly return a value, so the instance object is returned:

      function Foo(){
      this.color = “red”;
      }

      console.dir(new Foo()) // object -> color : red

      Please let me know if you still have any questions about this.

      Best Wishes,

      Kevin

  • Vivek

    Hey Kevin thanks for your explanation regarding the topic. Am little bit confused with ‘closures’, if you don’t mind, can you explain please.

  • Gaurav

    Nice article. Thanks for covering up the basic details of Javascript. But , I think you should have included a bit on closures 🙂

Comments are closed.