We make decisions everyday from what to eat, what to do first when we wake up, etc. In order to do this we often ask ourselves simple yes or no questions to make decisions. In order to help computers make decisions, we use conditional statements to tell the computer what to do at various points in our program.
Conditional statements are programming commands that are used to handle decisions and control program flow. We use the keywords if
and else
to define a conditional statement in JavaScript. Conditional statements in JavaScript follow a similar syntax as conditional statements in Java.
if (condition) {
code to run if condition is true
} else {
run some other code instead
}
if
followed by some parentheses.true
or false
.true
.else
.true
— or in other words, the condition is false
.Source: MDN Docs on conditionals