LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 18

Arrays compared with == and + Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 18 Back to Exercise

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

main.js
console.log([1, 2] == "1,2", [1, 2] === [1, 2], [] + 1, [5] * 2);
  • A false false 1 10
  • B true false 1 10 Correct
  • C true true 1 NaN
  • D false false NaN NaN
Correct Answer: Option B

Arrays convert to strings when a primitive is involved: [1, 2] → "1,2", [] → "" ("" + 1 = "1"), [5] → "5" → 5 for *. Two arrays are compared by reference.

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