if Statement

The if Statements

if Statement

🙋 Need help? Ask an expert now!

The if statement is the most simple conditional statement. It enables you to test for a certain conditions and branch to different segments of code depending on the result. The if statement consists of two major components, the condition as well as the statements. The condition can be a boolean variable, a logical expression, a comparison expression, or anything that results in either true of false. The statement(s) are the code enclosed in the braces that run when the branch is selected.

The if statement is pretty straightforward. The condition is evaluated, and if the result is true, the statements are executed. If the result is false, the statements are not executed.

As shown above, the expression numberTwo > numberOne results in false because 24 is not larger than 123. This is why the statement biggerNumber = numberTwo is not executed. Instead, biggerNumber = numberOne is executed because the expression numberOne > numberTwo is true. As a result, 123 is printed, which is what we expect given the context of the message in console.log(). Try playing with the if statements and print the smaller number instead.

Edit Me on GitHub!

Application Question

Consider the following code segment:

if (10 > 20 && 20 > 10){
    console.log(10);
}
if (10 < 20 && 50 / 5 == 10){
    console.log(20);
}
if (10 == 20 || a / 0){
    console.log(30);
}

What will the following code print to the console?