If Statement Function
How to validate a condition in javascript?
What is the use of if statement?
Explanation
If statement is used to check or verify a condition and execute a set of statement only if the condition is true. This should be referred as a statement and not as a function.
Syntax:if(condition)
{
// set of statements that will be executed
// only if the condition is true or satisfied
}Example:
<script language="javascript">
var a = "if check";
if(a == "if check")
{
document.write(" inside if statement ");
}
</script>
Result:
In the above example the condition is to check whether variable 'a' equals (==) the string "if check". As the condition satisfies, the statements inside the brackets are executed.
Nested If:
Nested if statement is nothing but using an if statement inside another if statement. Nested if is used when we check a condition only when another condition is true. For an example when we purchase a car, first we verify is the car looks good, only if it satisfies we go to the next condition color, then next and so on..
Syntax: Nested if
if(condition 1)
{
if(condition 2)
{
// set of statements that will be executed
}
}