Conditional Statements
Conditional statements allow you to run a bit of code if a certain condition is satisfied. Conditional statements are an important part of structured programming and let us make much more dynamic software.
There are a number of operators that help use make certain conditions, and these conditions will result in a boolean value for us to evaluate.
NOTE: the assignment operator = is different to the conditional operator ==. event though they look similar, the single one is for assigning values, and the double one is for comparing values.
consider the following code:
1
var userAge = window.prompt('How old are you?');
2
​
3
if (userAge >= 18) {
4
alert('You are an adult');
5
} else {
6
alert('you are legally a child');
7
}
Copied!
Conditional statements make use of the if/else keywords to set up control flow.
If the condition inside the parenthesis next to the if keyword evaluate to true, then that block will run, otherwise the else block will run if present (you don't need to include an else block)
Last modified 1yr ago
Copy link