# If Statement

The if statement is a conditional tester used in ensuring the execution of a code block only when a condition or expression is true. It is similar to our real life usage of the word "if". For example, "if you are done, turn off the system". The statement is true when the individual in question is "done". Same way do you use the "if" statement in executing a statement(s) only when a condition is true. For example, if "8 is equal to 5", then do this or that.
``````var num = 5;
if (num == 10){
}``````
In the above, we used the "if" conditional tester to test if the value of the variable (num) is equal to 10. If it is true (i.e the variable value is equal to 10), then, the statement contained in the code block ({}) will be executed.
``````<script>
}
</script>``````
If Statements With Logical Operators Conditions being tested in an "if" statement can be put together using logical operators and together, form a larger expression to be tested. You can learn more on logical operators here.
`````` if (4 > 2 && 7 < 12){ /*This is true if
4 is greater than 2 and 7 is less then 12
*/
}``````
EXAMPLE
``````<script>
}
</script>``````
= And == Most beginners do make mistakes of using the assignment operator(=) in place of the comparison operator(==). For example, a beginner may want to test if the value of a variable is same as that of a value using the assignment operator(=) because that is what we have been familiar with in our every day live mathematics.
``````var x = 5;
if (x = 2){
}``````
Now, the condition above will always be true because it is same as saying that "if 2 has been assigned to variable 'x', alert 1". The assignment will always be done. Therefore, take note of the difference between the assignment operator(=) and the comparison operator(==). The assignment operator(=) is used in assigning a value to a variable while the comparison operator(==) is used in comparing two values in order to determine if they possess same values. SUMMARY
1. The if statement is used in testing a statement or expression.
2. if the state of the expression returns true, then a code block is executed.
3. The expression to be tested in an if statement can be made sophisticated with the use of various logical operators.
Take a short quiz on If Statement.
Ask a question or tell us if If Statement is easy or not.
cent?