JavaScript Conditionals Flashcards
What is Control Flow?
Control flow is the order in which statements are executed in a program. The default control flow is for statements to be read and executed in order from left-to-right, top-to-bottom in a program file.
What are Control Structures?
Control structures such as conditionals (“if” statements and the like) alter control flow by only executing blocks of code if certain conditions are met. These structures essentially allow a program to make decisions about which code is executed as the program runs.
What is the Logical Operator “ll”?
The logical OR operator “ll” checks two values and returns a boolean. If ONE or BOTH values are truthy, it returns “true”. If BOTH values are falsy, it returns “false”. Example: 10 > 5 ll 10 > 20 // true. Example: 10 > 100 ll 10 > 20 // false
What is a Ternary Operator?
The ternary operator allows for a compact syntax in the case of binary (choosing between two choices) 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 executed. Otherwise, the second expression is executed. Example: day === “Monday” ? price -= 1.5 : price += 1.5;
What is an “else” statement?
An “else” block can be added to an “if” block or series of “if”-“else-if” blocks. The “else” block will be executed ONLY if the “if” condition fails.
Example:
const isTaskCompleted = false;
if (isTaskCompleted) {
console.log(‘Task completed’);
} else {
console.log(‘Task incomplete’);
}
What is the Logical Operator “&&”?
The logical AND operator “&&” checks two values and returns a boolean. If BOTH values are truthy, then it returns “true”. If one, or both, of the values is falsy, then it returns “false”.
Example:
1 > 2 && 2 > 1; // false
4 === 4 && 3 > 1; // true
What is an “if” statement?
An “if” statement accepts an expression with a set of parentheses: 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.
Example:
const isMailSent = true;
if (isMailSent) {
console.log(‘Mail sent to recipient’);
}
What is the Logical Operator “!”?
The logical NOT operator “!” can be used to do one of the following: Invert a Boolean value. Invert the truthiness of non-Boolean values.
Example:
let lateToWork = true;
let oppositeValue = !lateToWork;
console.log(oppositeValue);
// Prints: false
What are Comparison Operators?
Comparison operators are used to compare two values and return “true” or “false” depending on the validity of the comparison.
What is the “strict equal” operator?
===
What is the “strict not equal” operator?
!==
What is the “greater than” operator?
>
What is the “greater than or equal to” operator?
> =
What is the “less than” operator?
<
What is the “less than or equal to” operator?
<=