condition ? exprIfTrue : exprIfFalse
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon (:),andfinally the expression to execute if the condition is falsy
// Conditional (Ternary) operators are a way to write if statements in a more compact way
// unary operators are used to change the value of a variable
// examples of unary operators are ++,--, !, typeof, delete
// binary operators are used to compare two values
// examples of binary operators are ==,===,!=,!==,>,<,>=,<=,+,-,*,/,%
const value =1<2 ? "yes":"no"
console.log(value)
MALE =True
FEMALE =False# (if_test_is_false, if_test_is_true)[test]
gender =("female","male")[MALE]print("You are a ","male")# Output: You are a male# (if_test_is_false, if_test_is_true)[test]
gender =("female","male")[FEMALE]print("You are a ","female")# Output: You are a female
condition =True# (if_test_is_false, if_test_is_true)[test]print(2if condition else1/0)#Output is 2
condition =False# (if_test_is_false, if_test_is_true)[test]print(2if condition else5)#Output is 5