LearnFrenzy
Sign in Take a free test

Operators · Predict the Output · Question 16

A nested ternary Answer, explanation & discussion

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

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

main.js
const marks = 72;
const grade = marks >= 90 ? "A" : marks >= 75 ? "B" : marks >= 60 ? "C" : "D";
console.log(grade);
  • A B
  • B D
  • C A
  • D C Correct
Correct Answer: Option D

The ternary is right-associative, so the conditions are tested in order: 72 is not ≥ 90, not ≥ 75, but is ≥ 60.

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