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 ( : ), and finally 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(2 if condition else 1/0)
#Output is 2
condition = False
# (if_test_is_false, if_test_is_true)[test]
print(2 if condition else 5)
#Output is 5