const age = 15;
(age >= 18) ?
console.log("can vote.") : console.log("cannot vote.");
Output
cannot vote.
// ternary operator to check the eligibility to vote
const age = 15;
const result = (age >= 18) ? " can vote." : "cannot vote.";
console.log(result);
Output
cannot vote.
Can you create a program to check whether a number is positive or negative?
const number = 10;
const result = (number >= 0) ?
"The number is positive" : "The number is negative";
console.log(result);
Output
The number is positive
Q. What is the correct ternary equivalent of the following if...else statement?
if (5 > 3) {
greater = 5;
}
else {
greater = 3;
}
- greater = 5 > 3 ? 3 : 5;
- 5 > 3 ? greater = 5 : 3;
- greater = 5 > 3 ? 5 : 3;
- 5 > 3 ? greater = 3 : 5;
Answer: 3