LearnFrenzy
Sign in Take a free test

String Methods · Predict the Output · Question 9

match when nothing matches Answer, explanation & discussion

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

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

main.js
const found = "abc".match(/\d/g);
console.log(found, found?.length ?? 0, ("abc".match(/\d/g) || []).length);
  • A [] 0 0
  • B undefined 0 0
  • C null undefined 0
  • D null 0 0 Correct
Correct Answer: Option D

match returns null – not an empty array – so length must be guarded with ?. / ?? or by defaulting to [].

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