0
0
Javascriptprogramming~20 mins

Arrow functions in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Arrow Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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));
Aundefined
BSyntaxError
CNaN
D20
Attempts:
2 left
πŸ’‘ Hint
Remember that arrow functions without braces return the expression automatically.
❓ Predict Output
intermediate
2:00remaining
Arrow function and this keyword
What will this code output?
Javascript
const obj = {
  value: 10,
  getValue: () => this.value
};
console.log(obj.getValue());
Aundefined
B10
CTypeError
Dnull
Attempts:
2 left
πŸ’‘ Hint
Arrow functions do not have their own this; they use the this from the surrounding scope.
❓ Predict Output
advanced
2: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));
Aundefined
BSyntaxError
C{ name: 'Alice', age: 30 }
D[object Object]
Attempts:
2 left
πŸ’‘ Hint
To return an object literal from an arrow function, wrap it in parentheses.
❓ Predict Output
advanced
2: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));
A9
BTypeError
C7
D6
Attempts:
2 left
πŸ’‘ Hint
Count how many extra arguments are in rest and add them to a + b.
❓ Predict Output
expert
2: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();
Aundefined
B3
CTypeError
D0
Attempts:
2 left
πŸ’‘ Hint
Arrow functions keep the this from the Counter function context.