Learn JavaScript_ Conditionals Cheatsheet _ Codecademy
Learn JavaScript_ Conditionals Cheatsheet _ Codecademy
Conditionals
Control Flow
Logical Operator ||
The logical OR operator || checks two values and true || false; // true
returns a boolean. If one or both values are truthy, it
10 > 5 || 10 > 20; // true
returns 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)
let day = "Monday";
decisions. 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 day === "Monday" ? price -= 1.5 : price
executed, otherwise, the second expression is
+= 1.5;
executed.
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-control-flow/cheatsheet 1/4
6/25/24, 10:18 PM 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
1 > 2 && 2 > 1; // false
returns true . If one, or both, of the values is falsy, then
it returns false . true && false; // false
4 === 4 && 3 > 1; // true
switch Statement
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-control-flow/cheatsheet 2/4
6/25/24, 10:18 PM Learn JavaScript: Conditionals Cheatsheet | Codecademy
if Statement
Logical Operator !
The logical NOT operator ! can be used to do one of let lateToWork = true;
the 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
6/25/24, 10:18 PM Learn JavaScript: Conditionals Cheatsheet | Codecademy
else if Clause
After an initial if block, else if blocks can each const size = 10;
check 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