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:

var userAge = window.prompt('How old are you?');

if (userAge >= 18) {
    alert('You are an adult');
} else {
    alert('you are legally a child');
}

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 updated