Challenge - 5 Problems
Arrow Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of arrow function with implicit return
What is the output of this code?
Javascript
const multiply = (a, b) => a * b; console.log(multiply(4, 5));
Attempts:
2 left
π‘ Hint
Remember that arrow functions without braces return the expression automatically.
β Incorrect
The arrow function multiplies a and b and returns the result implicitly. So multiply(4,5) returns 20.
β Predict Output
intermediate2:00remaining
Arrow function and this keyword
What will this code output?
Javascript
const obj = { value: 10, getValue: () => this.value }; console.log(obj.getValue());
Attempts:
2 left
π‘ Hint
Arrow functions do not have their own this; they use the this from the surrounding scope.
β Incorrect
The arrow function uses the this from the global scope, where value is undefined, so it returns undefined.
β Predict Output
advanced2:00remaining
Arrow function returning object literal
What is the output of this code?
Javascript
const createUser = (name, age) => ({ name: name, age: age }); console.log(createUser('Alice', 30));
Attempts:
2 left
π‘ Hint
To return an object literal from an arrow function, wrap it in parentheses.
β Incorrect
The arrow function returns an object with properties name and age. The parentheses ensure the object is returned, not treated as a block.
β Predict Output
advanced2:00remaining
Arrow function with default parameters and rest parameters
What does this code output?
Javascript
const sum = (a = 1, b = 2, ...rest) => a + b + rest.length; console.log(sum(3, 4, 5, 6));
Attempts:
2 left
π‘ Hint
Count how many extra arguments are in rest and add them to a + b.
β Incorrect
a=3, b=4, rest=[5,6] so rest.length=2. Sum is 3+4+2=9.
β Predict Output
expert2:00remaining
Arrow function and lexical this in nested functions
What is the output of this code?
Javascript
function Counter() { this.count = 0; setInterval(() => { this.count++; if (this.count === 3) { console.log(this.count); clearInterval(this.intervalId); } }, 100); this.intervalId = setInterval(() => {}, 1000); } new Counter();
Attempts:
2 left
π‘ Hint
Arrow functions keep the this from the Counter function context.
β Incorrect
The arrow function inside setInterval uses the this of Counter, so this.count increments correctly. When count reaches 3, it logs 3 and clears the interval.