Last active
February 23, 2022 15:28
-
-
Save codistwa/65ed9f8823010543550a99dd566f8b97 to your computer and use it in GitHub Desktop.
Course source code: https://codistwa.com/guides/conditional-expressions-and-logical-operators. More courses on https://codistwa.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================================ | |
// Comparison operators | |
// ============================================================ | |
console.log(3 < 2); // false | |
console.log(1 == '1'); // true | |
console.log('Banana' === 'Banana'); // true | |
console.log('Banana' !== 'Apple'); // true | |
console.log('Banana' != 'Apple'); // true | |
console.log(2 <= 3); // true | |
console.log(3 >= 3); // true | |
// ============================================================ | |
// Logical Operators | |
// ============================================================ | |
const isSun = true; | |
const isCloud = false; | |
const isPlanet = false; | |
if (isSun || isCloud) { | |
console.log('It\'s sunny'); // It's sunny | |
console.log('There is no cloud'); // There is no cloud | |
} | |
if (isSun && isCloud) { | |
console.log('There is sun and clouds'); | |
} else { | |
console.log('There is no sun and clouds'); // There is no sun and clouds | |
} | |
if (!isPlanet) { | |
console.log('This is not a planet'); // This is not a planet | |
} | |
// ============================================================ | |
// Conditional testing | |
// ============================================================ | |
const isSunny = true; | |
const isRain = false; | |
// if | |
if (3 > 2) { | |
console.log('3'); // 3 | |
} else { | |
console.log('2'); | |
} | |
// if else if | |
if (isSunny) { | |
console.log('Sunglasses'); // Sunglasses | |
} else if (isRain) { | |
console.log('Umbrella'); | |
} else { | |
console.log('Just my coat'); | |
} | |
// switch | |
const color = 'red'; | |
switch (color) { | |
case 'yellow': | |
console.log('One yellow pencil'); | |
break; | |
case 'red': | |
console.log('One red pencil'); // One red pencil | |
break; | |
default: | |
console.log(`No ${color} pencil`); | |
} | |
// ============================================================ | |
// Shorcuts | |
// ============================================================ | |
const exampleTernary = 3 > 2 ? '3' : '2'; | |
const exampleBoolean = 3 > 2 || 'Hello'; | |
const exampleBooleanString = 3 > 2 && 'Hello'; | |
console.log(exampleTernary); // 3 | |
console.log(exampleBoolean); // true | |
console.log(exampleBooleanString); // Hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================ | |
# Comparison operators | |
# ============================================================ | |
print(3 < 2) # False | |
print(1 == '1') # False | |
print('Banana' == 'Banana') # True | |
print('Banana' != 'Apple') # True | |
print('Banana' != 'Apple') # True | |
print(2 <= 3) # True | |
print(3 >= 3) # True | |
# ============================================================ | |
# Logical Operators | |
# ============================================================ | |
is_sun = True | |
is_cloud = False | |
is_planet = False | |
if is_sun or is_cloud: | |
print('It\'s sunny') # It's sunny | |
print('There is no cloud') # There is no cloud | |
if is_sun and is_cloud: | |
print('There is sun and clouds') | |
else: | |
print('There is no sun and clouds') # There is no sun and clouds | |
if not is_planet: | |
print('This is not a planet') # This is not a planet | |
# ============================================================ | |
# Conditional testing | |
# ============================================================ | |
is_sunny = True | |
is_rain: False | |
# if | |
if 3 > 2: | |
print('3'); # 3 | |
else: | |
print('2'); | |
# if elif | |
if is_sunny: | |
print('Sunglasses') # Sunglasses | |
elif is_rain: | |
print('Umbrella') | |
else: | |
print('Just my coat') | |
# ============================================================ | |
# Shorcuts | |
# ============================================================ | |
exampleTernary = '3' if 3 > 2 else '2' | |
exampleBoolean = 3 > 2 or 'Hello' | |
exampleBooleanString = 3 > 2 and 'Hello' | |
print(exampleTernary) # 3 | |
print(exampleBoolean) # True | |
print(exampleBooleanString) # Hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment