Conditions and Comparisons

Sometimes computers have to make decisions to be able to solve problems, and programmers have to tell the computer what conditions and comparisons have to be made in order to come to a solution. The computer makes decisions by evaluating a condition and if the condition is true the computer will execute one or more statements, if the condition is false then another statement or other statements will be executed.

In JavaScript there are couple different ways to evaluate a condition. These are performed through the use of the following statement if, if else and switch, let’s take a look on this statements.

If statement

An if statement in JavaScript looks as follows:

    if (condition){
        Statement 1;
        Statement 2;
     } 

If the condition is true then Statement 1 and Statement 2 will be executed, if the condition is false then Statement 1 and Statement 2 will not be executed

if else statements

An if else statement in JavaScript looks as follows

    if (condition){
        Statement 1;
        Statement 2;
    }else{
       Statement 3;
    }

This case is similar to the previous example but in this case if the condition is true then Statement 1 and Statement 2 will be executed and if the condition is false then Statement 3 will be executed.

We can also combine the if else statements as follows.

    if (condition){
       Statement 1;
       Statement 2;
   }else if (condition2){
      Statement 3;
   }else{
       Statement 4;
   }

This example is similar to the previous one but this time we are telling our computer to evaluate condition 1 if condition 1 is not true then the computer goes and evaluate condition 2 in case condition 2 is true then Statement 3 will be executed and in case that condition1 and condition2 are not true then in any other case Statement 4 will be executed.

So far we have talk about the flow of the if and if else statements and you have notices that in each case there is a condition, so how do we build those conditions on JavaScript? Well in this case we will use something call relational operators. These operators are shown in the table below.

JavaScript Relational Operators

Operator Name Example
< less than if (i < 3)
> greater than if (i > 3)
<= less or equal to if (row <= end)
>= greater or equal to if (age >= 8)
== equal to if (col == last)
!= not equal to if (col != last)

Let’s illustrate the use of this operators and the if, and if else conditions we will also use variables, let’s consider the following situation to create a javaScript program.

Write a program that helps a user choose the correct foot wear for the day’s weather. The following table shows the weather types the user may enter and what your program should output.

Weather Foot Wear
hot sandals
rain galoshes
snow boots

If the user enters any other weather type, your program should output “shoes”.

Switch Statement

This is a statement that helps us perform different action depending on the different values a variable can take. How does this work?

Let’s suppose that we are building a calendar and we want to define the number of days a month has, as we know, some months have 30 days some have 31 days and February has 28 days or 29 days depending if the year is a leap year. For this example we will consider only the case when February has 28 days.