Conditionals Flashcards
Logical operators, if/else, switch statements
What is control flow?
The order in which statements are executed in a program.
Control structures such as conditionals (if statements and the like) alter control flow by only executing blocks of code if certain conditions are met.
OR || operator
Checks two values and returns a boolean.
*if one or both values are truthy = true
*if both values are falsy = false
AND && operator
Checks two values and returns a boolean
*if both values are truthy = true
*if one or both values are falsy = false
Ternary Operator
Allows for a compact syntax in the case of binary
It accepts a condition followed by ?, then two expressions separated by :
If the condition is truthy, the first expression is executed
If the condition is falsy, the second expression is executed
let price = 10.5;
let day = “Monday”;
day === “Monday” ? price -= 1.5 : price += 1.5;
if statement
Accepts an expression with a set of parentheses
*if the expression is truthy, the code executes
* if the expression is falsy, the code will not execute
else statement
Can be added to an if block or statement, and will be evaluated only if the if condition is falsy
switch statement
Checks an expression against multiple case clauses. If a case matches, the code inside that clause is executed.
The case clause should finish with a break keyword. If no case matches but a default clause is included, the code inside the default will be executed.
Not ! operator
Can be used to invert a boolean value or invert the truthiness of non-boolean values
let lateToWork = true;
let oppositeValue = !lateToWork; //false
Comparison Operators
=== strict equal
!== strict not equal
> greater than
>= greater than or equal
< less than
<= less than or equal
Truthy and Falsy Values
Values that are evaluated to true are known as truthy
Values that are evaluated to false are known as falsy
Falsy values include:
false
0
empty strings
null
undefined
NaN.
All other values are truthy.