Learn JavaScript - Conditionals Cheatsheet - Codecademy
Learn JavaScript - Conditionals Cheatsheet - Codecademy
Conditionals
Control Flow
Logical Operator ||
The logical OR operator || checks two values and returns true || false; // true
a boolean. If one or both values are truthy, it returns
10 > 5 || 10 > 20; // true
true . If both values are falsy, it returns false .
false || false; // false
A B A || B
10 > 100 || 10 > 20; // false
false false false
Ternary Operator
The ternary operator allows for a compact syntax in the let price = 10.5;
case of binary (choosing between two choices) decisions.
let day = "Monday";
It accepts a condition followed by a ? operator, and
then two expressions separated by a : . If the condition
evaluates to truthy, the first expression is executed, day === "Monday" ? price -= 1.5 : price +=
otherwise, the second expression is executed.
1.5;
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-control-flow/cheatsheet 1/4
1/18/24, 10:42 AM Learn JavaScript: Conditionals Cheatsheet | Codecademy
else Statement
The logical AND operator && checks two values and true && true; // true
returns a boolean. If both values are truthy, then it returns
1 > 2 && 2 > 1; // false
true . If one, or both, of the values is falsy, then it returns
false . true && false; // false
4 === 4 && 3 > 1; // true
switch Statement
🦪
console.log('The taste of the sea
Note: If break is omitted from the block of a case , the
');
switch statement will continue to check against case
values until a break is encountered or the flow is broken. break;
🍕
case 'pizza':
console.log('A delicious pie ');
break;
default:
console.log('Enjoy your meal');
}
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-control-flow/cheatsheet 2/4
1/18/24, 10:42 AM Learn JavaScript: Conditionals Cheatsheet | Codecademy
if Statement
Logical Operator !
The logical NOT operator ! can be used to do one of the let lateToWork = true;
following:
let oppositeValue = !lateToWork;
Invert a Boolean value.
Invert the truthiness of non-Boolean values.
console.log(oppositeValue);
// Prints: false
Comparison Operators
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-control-flow/cheatsheet 3/4
1/18/24, 10:42 AM Learn JavaScript: Conditionals Cheatsheet | Codecademy
else if Clause
After an initial if block, else if blocks can each check const size = 10;
an additional condition. An optional else block can be
added after the else if block(s) to run by default if none
of the conditionals evaluated to truthy. if (size > 100) {
console.log('Big');
} else if (size > 20) {
console.log('Medium');
} else if (size > 4) {
console.log('Small');
} else {
console.log('Tiny');
}
// Print: Small
Print Share
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-control-flow/cheatsheet 4/4