Are you ready?
11 questions to solve
Instructions
- This quiz goes to full-screen once you press the Start button.
- At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
Is the following a valid arrow function?
x => x;
Yes, the function is valid. It has a single parameter and simply returns the parameter. (Quite often, such a function is referred to as an identity function.) For more info, refer to JavaScript Arrow Functions.
Is the following a valid arrow function?
() => true;
Yes, the function is valid. It has no parameters (hence the parentheses (
()
)) and simply returns the Boolean true
. For more info, refer to JavaScript Arrow Functions: Syntax of arrow functions.Is the following a valid arrow function?
_ => true;
Yes, the function is valid. It has a parameter called
_
(remember that _
is valid in naming identifiers in JavaScript) and simply returns the Boolean true
. For more info, refer to JavaScript Arrow Functions: Syntax of arrow functions.Suppose we wish to return the object {x: 10}
from an arrow function.
Is the following a valid way to do so?
() => { x: 10 };
No. The object literal here would be treated as the body of the arrow function, containing the code
x: 10
in it. The correct way to represent the object literal is to encapsulate it inside a pair of parentheses (()
). Hence, the correct choice is (B). For more info, refer to JavaScript Arrow Functions: Returning an object literal.An arrow function can contain a rest parameter. True or false?
Absolutely true. In fact, by not creating a local
arguments
object, an arrow function actually encourages the use of rest parameters. For more info, refer to JavaScript Arrow Functions: No own arguments
object.What is the value of
y
in the following code?var y = [1, 2, 3].map(x => x ** 2);
The mapping function maps each number of the given array to its square. Hence,
[1, 2, 3]
becomes [1, 4, 9]
, and this goes with choice (C). For more info, refer to JavaScript Arrow Functions: Examples.What is the value of
y
in the following code?var y = [1, 2, 3].map(x => { x ** 2 });
The mapping function maps each number of the given array to
undefined
, since there is no explicit return
in the function's body (denoted via the curly braces ({}
)). Hecne, the correct choice is (D). For more info, refer to JavaScript Arrow Functions: Examples.An arrow function 'closes over
this
' from its outer lexical environment. What does this mean?To 'close over
this
' simply means that an arrow function obtains its this
from its outer lexical environment. It doesn't create a this
binding of its own unlike a regular function in JavaScript. Hence, the correct choice is (B). For more info, refer to JavaScript Arrow Functions: Closing over this
.The
arguments
object is created locally for every arrow function. True or false?False. Arrow functions don't create their own local
arguments
object unlike regular functions in JavaScript, perhaps to encourage the much better way of using rest parameters. For more info, refer to JavaScript Arrow Functions: No own arguments
object.What does the following code log?
console.log(() => arguments);
The code actually throws an error as the arrow function returns an undefined variable
arguments
. Hence, the correct choice is (C). For more info, refer to JavaScript Arrow Functions: No own arguments
object.What does the following code log?
function f() {
return () => {
console.log(arguments);
};
}
var f2 = f(10, 20, 30);
f2(1, 2, 3);
The call to
f2(1, 2, 3)
(in the last line) logs the arguments
object. Since, the function f2()
itself doesn't define arguments
(as it's an arrow function), its value is taken from the outer environment where arguments
holds the list [10, 20, 30]
. Hence, the correct choice is (A). For more info, refer to JavaScript Arrow Functions: No own arguments
object.