JavaScript Resources

if Conditional Statement

The if conditional statement is used when, you want a set of statements to execute if and only if a condition is met. If the condition is not met, then the statements are ignored.

The if conditional statement is known as a control structure. It comprises several pieces of code that work together to produce a result. It looks like this:

    if (some condition that evaluates to true) {

      then execute this statement

      and any other statements on additional lines

    }

Think of the if conditional statement as a way of providing an alternative option in your code that may execute for some users.

if can be used on its own, or in collaboration with the else and else if conditional statements.

Just to be clear. The above example is not a real working example. The core pieces of the conditional stateement are if, the parentheses (), and the curly braces {}. Later in this introduction, we will look at examples of conditions and statements that you can put in place of the placeholder text above.

Next up ... the "else if" Conditional Statement