If statement

Let's say John has 23 apples and Tom has 45 apples. We want to check who has more apples using JavaScript programming. We need to make our browser understand the if statement.

Note

The if statement compares two variables.

To check our condition, we need to declare the two variables containing the number of apples, as follows:

var john = 23;
var tom = 45;

To check which number is bigger, we can apply the if statement as shown in the following:

if(john > tom)
{
  alert("John has more apples than tom");
}

Let's say that we do not know which variable is bigger. Then, we need to check both the variables. Therefore, we need to include the following codes to our program:

if(tom > john )
{
  alert("Tom has more apples than John");
}

The whole code in ...

Get JavaScript Projects for Kids now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.