Course: JavaScript

Progress (0%)

  1. Foundation

  2. Numbers

  3. Strings

  4. Conditions

  5. Loops

  6. Arrays

  7. Functions

  8. Objects

  9. Exceptions

  10. HTML DOM

  11. CSSOM

  12. Events

  13. Drag and Drop

  14. opt Touch Events

  15. Misc

  16. Project: Analog Clock

JavaScript Function Methods Quiz

Quiz 15 13 questions

Prerequisites for the quiz

  1. JavaScript Function Methods

Are you ready?

13 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What does the following code log?
function a() { console.log(this.x); }

var x = 10;
a.x = 20;

a.call(a);
a.call(a) would call a() with its this value set to the object a. This means that this.x would simply translate to a.x, which would ultimately log 20. This goes with choice (B). For more info, refer to JavaScript Function Methods — call().
What does the following code log?
function a() { console.log(this.x); }

var x = 10;
a.x = 20;

a.call();
a.call() calls a() with its this set to undefined. Since, the script is running in non-strict mode, this is reset to the global window object. This means that this.x translates to window.x, which simply logs the value of the global variable x i.e 10.

This goes with choice (A). For more info, refer to JavaScript Function Methods — call().
What does the following code log?
function a() { console.log(this.x); }

var x = 10;
a.x = 20;

a.call(null);
a.call(null) calls a() with its this set to null. Since, the script is running in non-strict mode, setting this to null is ignored and instead the window object is used as this. This means that this.x translates to window.x, which simply logs the value of the global variable x i.e 10.

This goes with choice (A). For more info, refer to JavaScript Function Methods — call().
For a given function object f, f.call(f) and f.apply(f) yield the same result. True or false?
Both the function methods call() and apply() would call f() in the statement given above with its this set to f. This means that, yes, both would yield the same result (in this case). The correct choice therefore is (A). For more details, refer to JavaScript Function Methods — call() and JavaScript Function Methods — apply().
The call() method takes arguments for the underlying function in the form of an array. True or false?
This is clearly false. It's the apply() function method that takes arguments for the underlying function in the form of an array — not the call() method. Hence, the correct choice is (B). For more details, refer to JavaScript Function Methods — apply().
What does the following code log?
function f() {
    console.log(arguments.join());
}

f(10, 20, 30);
The arguments object available inside a function is not an array, and likewise doesn't have access to array methods such as slice(), join(), indexOf() and so on. Hence, in the code above, arguments.join() would throw an error as it's invalid to invoke the value undefined i.e. the value of the expression arguments.join. This goes with choice (C). For more details, refer to JavaScript Function Arguments — The arguments object.
What does the following code log?
function f() {
    console.log([].join.call(arguments));
}

f(10, 20, 30);
The expression [].join.call(arguments) calls the join() array method with its this set to arguments, and without any arguments for itself. This means that the items in arguments are joined together into a string with the delimiter ',', yielding the string 10,20,30 in the case above.

This goes with choice (A). For more details, refer to JavaScript Function Methods — call().
What does the following code log?
function f() {
    console.log(arguments.call.join(arguments));
}

f(10, 20, 30);
call() is a function method and arguments is not a function. Hence, in the code above, the expression arguments.call.join throws an error, since it's invalid to access a property on the value undefined i.e. the value of the expression arguments.call.

This goes with choice (C). For more details, refer to JavaScript Function Methods — call().
What does the following code log?
var min = Math.min.apply([1, 5, 30, 0]);
console.log(min);
Math.min.apply([1, 5, 30, 0]) calls Math.min() with its this set to [1, 5, 30, 0] and with no arguments of its own. Since Math.min() doesn't use this in its internal definition, this is equivalent to calling Math.min(), which returns Infinity.

Hence, the correct choice is (C). For more details, refer to JavaScript Function Methods — apply().
What does the following code log?
var min = Math.min.apply(this, [1, 5, 30, 0]);
console.log(min);
Math.min.apply(this, [1, 5, 30, 0]) calls Math.min() with the arguments 1, 5, 30 and 0; and with its this set to this of the global scope i.e. the window object. Math.min() doesn't use this in its internal definition, likewise the first argument to apply() here is not of any special importance. The minimum of the given numbers is 0, and that's exactly what is logged in the console.

This goes with choice (A). For more details, refer to JavaScript Function Methods — apply().
What does the following code log?
var nums = [1, 2, 3];
nums.push.apply(nums, [4, 5, 6]);

console.log(nums);
nums.push.apply(nums, [4, 5, 6]) calls the push() array method with its this set to nums and with the arguments 4, 5, and 6. These arguments are added to the end of nums one-by-one to ultimately make it equal to [1, 2, 3, 4, 5, 6]. Remember that apply() converts its second array argument into a list of arguments and calls the underlying function with this list of arguments, NOT the passed-on array.

Hence, the correct choice is (A). For more details, refer to JavaScript Function Methods — apply().
What does the following code log?
var min = Math.min.bind(null, 0);

console.log(min(1, 20, 3), min());
Math.min.bind(null, 0) simply returns back a bound function wrapping over the Math.min() method, with its this set to null, and the first argument set to 0. This bound function is saved in the variable min. Calling min() with a list of arguments is therefore equivalent to calling Math.min() with 0 followed by that very list of arguments.

That is, min(1, 20, 3) is equivalent to Math.min(0, 1, 20, 3) which yields 0, whereas min() is equivalent to Math.min(0) which yields 0 as well. This goes with choice (A). For more details, refer to JavaScript Function Methods — bind().
What does the following code log?
function f() {
    console.log(typeof [].indexOf.bind(arguments, 0));
}

f(0, 1, 2);
Calling the bind() function method returns back a function. Hence, in the code above, the expression containing the bind() call would resolve down to a function, on which typeof would yield back 'function'. This goes with choice (C). For more details, refer to JavaScript Function Methods — bind().