xxxxxxxxxx
var foo = (
bar === 'a' ? 1 : // if
bar === 'b' ? 2 : // else if
bar === 'c' ? 3 : // else if
null // else
);
xxxxxxxxxx
// Use Multiple Conditional Ternary Operators Javascript.
function checkSign(num) {
return num > 0 ? "positive" : num < 0 ? "negative" : "zero";
}
console.log(checkSign(10));
console.log(checkSign(-10));
console.log(checkSign(0));
xxxxxxxxxx
const guessNumber = 8
const correctAnswer = 8
const message = guessNumber > correctAnswer ? 'The Guess was too High!'
: (guessNumber < correctAnswer) ? 'The Guess was too low!'
: 'Perfect guess!'
console.log(message)
xxxxxxxxxx
const lifePhase = (age) => {
age = +age;
return age <= 3
? "baby"
: age >= 4 && age <= 12
? "child"
: age >= 13 && age <= 19
? "teen"
: age >= 20 && age <= 64
? "adult"
: age >= 65 && age <= 140
? "senior citizen"
: "This is not a valid age";
};
console.log(lifePhase(5));
console.log(lifePhase(15));
console.log(lifePhase(25));
console.log(lifePhase(105));
console.log(lifePhase(0));
console.log(lifePhase(-155));
let myAge = lifePhase(38);
console.log(myAge);