JS Cheatsheet Conditionals
JS Cheatsheet Conditionals
Introduction to
Ja v a S c r i p t
Conditionals
Print cheatsheet
OR || Operator
1 of 9 2020-03-02, 22:52
https://www.codecademy.com/learn/introduction-to-javascript/...
2 of 9 2020-03-02, 22:52
https://www.codecademy.com/learn/introduction-to-javascript/...
// A: if/else
if (day === "Monday") {
price -= 1.5;
} else {
price += 1.5;
}
// B: ternary operator
day === "Monday" ? price -= 1.5 : price += 1.5;
else Statement
3 of 9 2020-03-02, 22:52
https://www.codecademy.com/learn/introduction-to-javascript/...
if (isTaskCompleted) {
console.log('Task completed');
} else {
console.log('Task incomplete');
}
switch Statement
4 of 9 2020-03-02, 22:52
https://www.codecademy.com/learn/introduction-to-javascript/...
The case clause should finish with a break keyword. If no case matches
but a default clause is included, the code inside default will be
executed. If break is omitted from the block of a case (or the execution
is not broken otherwise, such as returning from a function with a switch),
the switch statement will continue to check against case values until a
break is encountered or the flow is broken.
switch (food) {
case 'oyster':
console.log('Enjoy the taste of the sea');
break;
case 'pizza':
console.log('Enjoy a delicious pie');
break;
default:
console.log('Enjoy your meal');
}
if Statement
5 of 9 2020-03-02, 22:52
https://www.codecademy.com/learn/introduction-to-javascript/...
● If the expression evaluates to a truthy value, then the code within its
code body executes.
● If the expression evaluates to a falsy value, its code body will not
execute.
if (isMailSent) {
// This code block will be executed
console.log('Mail sent to recipient');
}
NOT ! Operator
6 of 9 2020-03-02, 22:52
https://www.codecademy.com/learn/introduction-to-javascript/...
// example 1
let value = true;
let oppositeValue = !value;
console.log(oppositeValue); // false
// example 2
const emptyString = '';
!emptyString; // true
const truthyNumber = 1;
!truthyNumber // false
7 of 9 2020-03-02, 22:52
https://www.codecademy.com/learn/introduction-to-javascript/...
1 > 3 // false
3 > 1 // true
250 >= 250 // true
1 === 1 // true
1 === 2 // false
1 === '1' // false
8 of 9 2020-03-02, 22:52
https://www.codecademy.com/learn/introduction-to-javascript/...
9 of 9 2020-03-02, 22:52