LearnFrenzy
Sign in Take a free test

Destructuring · Predict the Output · Question 10

Destructuring Map entries in a loop Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

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

main.js
const stock = new Map([["pen", 10], ["ink", 0], ["pad", 3]]);
for (const [item, qty] of stock) {
  if (!qty) console.log(`${item} out of stock`);
}
  • A pen out of stock
  • B ink out of stock Correct
  • C ink out of stock pad out of stock
  • D Nothing is printed
Correct Answer: Option B

Iterating a Map yields [key, value] pairs, which the loop head unpacks; only ink has a falsy quantity.

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