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 Basics Quiz

Quiz 13 10 questions

Prerequisites for the quiz

  1. JavaScript Function Basics

Are you ready?

10 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 an 'IIFE' stand for?
IIFE stands for Immediately Invoked Function Expression. This goes with choice (D). For more info, refer to JavaScript Functions — IIFEs.
What does the following code log?

There is no typo in the code.

(function() {
   console.log('Hello World!');
});
The given function expression is not invoked via (), hence it's body doesn't execute. This means that nothing is logged in the console, which goes with choice (B). For more info, refer to JavaScript Functions — IIFEs.
Differentiate between a function declaration and a function expression.
Only the first two descriptions are correct. For more info, refer to JavaScript Functions — Difference between function declarations and expressions.
How to recursively call an anonymous function that is passed as an argument to another function?
The callee property of the arguments object available inside a function contains a reference to the function itself, and can therefore be used to recursively call it. This goes with choice (B). For more info, refer to JavaScript Functions — arguments.callee.
What does the following code log?
console.log(this === window);
this as used in the global context returns back the window object. Hence, this === window evaluates to true, and thus the correct choice is (A). For more info, refer to JavaScript Functions — this.
What does the following code log?
var x = 10;
var f2 = f;

var obj = {
   x: 20,
   f: f2
}

console.log(f());
console.log(f2());
console.log(obj.f());

function f() {
   return this.x;
}

Let's go step-by-step. First, a global variable x is created with the value 10. Next another variable f2 is created with the value f, which is a function accessible at this stage via functional hoisting. Next up, an object obj is created with a property x set to 20 and a property f set to f2.

In the first log, f() gets executed with this.x resolving down to the global variable x. Likewise, we get 10 logged. The second log is similar to the first log, hence we get another 10 logged. The last log calls obj.f(), likewise this.x inside the function resolves to obj.x which is 20. Hence, 20 gets logged.

Altogether, we get 10, 10 and 20 logged. This goes with choice (C). For more info, refer to JavaScript Functions — this.

What is the problem in the following code?
function() {
}
The function definition above denotes a function declaration, hence its name is required. Since there is no name in the code, this is a problem that'll lead to a syntax error. Therefore, the correct choice is (B). For more info, refer to JavaScript Functions — this.
All functions in JavaScript are hoisted. True or false?
Clearly, this proposition is false. In particular, function expressions aren't hoisted. For more info, refer to JavaScript Functions — Hoisting.
What is the benefit of using an IIFE to encapsulate the entire code of a program?
An IIFE helps to prevent name collisions between multiple scripts as used on an HTML page. This goes with choice (B). For more info, refer to JavaScript Functions — IIFEs.
What does the following code log?
var sum = function localSum(a, b) {
   return a + b;
}

console.log(typeof localSum);
The name of a function created via a function expression is not available outside the body of the function. Hence, typeof localSum above returns 'undefined', and this goes with choice (A). For more info, refer to JavaScript Functions — Named function expressions.