If Statements

Conditional statements are often needed in programming. You may want to execute a block of code only if certain conditions are met. If statements are used in such situations. This lesson will cover two forms of if statements:

1. If Statement
If statement is a conditional statement. It checks the condition and if the condition evaluates to true, subsequent code block is executed. For example:

tutorial

In this example, we write a message to the page only if the score is greater than or equal to 70.
As you may have guessed, document.write() writes content into the document.

2. If...Else Statement
If...Else statement checks a condition. If it resolves to true, first code block is executed. If it resolves to false, second code block is executed. For example:

tutorial

This example is similar to the previous one. However, with an added step. If the score is greater than or equal to 70, it writes one message. And if the score is less than 70, it prints a different messsage.

NEXT LESSON