Course Content
Introduction to JavaScript
Now we will consider the most important field of application of boolean type.
Conditions
Conditions in JavaScript provide flow control. The interpreter executes commands in order, but often programmers need to control the execution flow. In some situations, it is necessary to branch the program's behavior based on certain conditions.
There are two keywords that can help you: if
and else
if
The if
keyword takes a boolean literal and opens a code block that will be executed if the taken literal is true
:
In the example above, you can see that the if
operator executes code when the taken literal is true
.
The syntax of the conditional operator is so simple: if
keyword, literal in the parentheses ()
and a code block in the curly brackets {}
.
The {
symbol opens the code block, and the }
symbol closes it.
The literal can be replaced by the expression:
You can see the 4 conditions in the example above where a = 935
:
Condition | Condition result | Code has been executed? |
a > 17 | true | Yes |
a > 235124 | false | No |
a > 0 | true | Yes |
a < 0 | false | No |
You are not limited to performing operations inside the code block:
Section 4.
Chapter 1