LearnFrenzy
Sign in Take a free test

Arrow Functions · Predict the Output · Question 11

Defaults and rest in an arrow Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const f = (a, b = a * 2, ...rest) => [a, b, rest.length];
console.log(f(1), f(1, 5, 7, 8));
  • A [ 1, 2, 0 ] [ 1, 5, 3 ]
  • B [ 1, undefined, 0 ] [ 1, 5, 2 ]
  • C SyntaxError
  • D [ 1, 2, 0 ] [ 1, 5, 2 ] Correct
Correct Answer: Option D

Arrows support defaults (which may use earlier parameters) and rest parameters exactly like regular functions.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links