We’ll be covering the following concepts:
- if, else if, and else statements
- comparison operators
- logical operators
- truthy vs falsy values
- ternary operators
- switch statement
So if you’re ready to learn these concepts go to the next lesson— else, read over the concepts, observe the diagram, and prepare yourself for this lesson!
The if statement is composed of:
- The if keyword followed by a set of parentheses () which is followed by a code block, or block statement, indicated by a set of curly braces {}.
- Inside the parentheses (), a condition is provided that evaluates to true or false.
- If the condition evaluates to true, the code inside the curly braces {} runs, or executes.
- If the condition evaluates to false, the block won’t execute.
The else statement:
- Uses the else keyword following the code block of an if statement.
- Has a code block that is wrapped by a set of curly braces {}.
- The code inside the else statement code block will execute when the if statement’s condition evaluates to false.
Here is a list of some handy comparison operators and their syntax:
- Less than: <
- Greater than: >
- Less than or equal to: <=
- Greater than or equal to: >=
- Is equal to: ===
- Is not equal to: !==
here are three logical operators:
- the and operator (&&)
- the or operator (||)
- the not operator, otherwise known as the bang operator (!)
Falsy values— or evaluate to false when checked as a condition. The list of falsy values includes:
- 0
- Empty strings like "" or ''
- null which represent when there is no value at all
- undefined which represent when a declared variable lacks a value
- NaN, or Not a Number
Проверка на логические условия идёт слева направо, что можно использовать с ИЛИ:
Ternary Operator
- The condition, isNightTime, is provided before the ?.
- Two expressions follow the ? and are separated by a colon : .
- If the condition evaluates to true, the first expression executes.
- If the condition evaluates to false, the second expression executes.
The else if statement always comes after the if statement and before the else statement. The else if statement also takes a condition.
if/else if/else statements are read from top to bottom, so the first condition that evaluates to true from the top to bottom is the block that gets executed.
The switch keyword
- The switch keyword initiates the statement and is followed by ( ... ), which contains the value that each case will compare. In the example, the value or expression of the switch statement is groceryItem.
- Inside the block, { ... }, there are multiple cases. The case keyword checks if the expression matches the specified value that comes after it. The value following the first case is 'tomato'. If the value of groceryItem equalled 'tomato', that case‘s console.log() would run.
- The value of groceryItem is 'papaya', so the third case runs— Papayas are $1.29 is logged to the console.
- The break keyword tells the computer to exit the block and not execute any more code or check any other cases inside the code block. Note: Without break keywords, the first matching case will run, but so will every subsequent case regardless of whether or not it matches—including the default. This behavior is different from if/else conditional statements that execute only one block of code.
- At the end of each switch statement, there is a default statement. If none of the cases are true, then the code in the default statement will run.
Если break нет, то выполнение пойдёт ниже по следующим case, при этом остальные проверки игнорируются. (https://learn.javascript.ru/switch)