JavaScript Resources

Conditions

The real power of conditional statements lies in the condition - an expression that evaluates to either true or false.

A condition is generally a simple logical or mathematical comparison, similar to the kind that you work with in basic arithmetic. It usually consists of two values (or expressions) separated by a comparison sign, such as an equals, greater than, or less than sign, etc..

Expressions are first evaluated on either side of the comparison sign, and then the two distinct values are compared. When compared, the entire expression will turn out to be either true or false.

For example:

    a numerical comparison:

      if (userSalary > 30000) {

        alert("You are treating us to dinner!")

      }

    a text comparison:

      if (userName == "Han Solo") {

        alert("Where's Chewbacca?")

      }

In the above example, userName and userSalary are variables that have been defined elsewhere, for example, by prompting the user.

Now, once a condition evaluates to either true or false, the rest of the conditional statement takes over and decides the next step. If the condition evaluates to true, then it executes the code between the curly braces {}. If the condition evalues to false, it ignores the code between the curly braces.

Next up ... Comparison Operators