Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

ternary operator

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
Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #ternary #operator
ADD COMMENT
Topic
Name
7+2 =