If-Else Statement:
If else statement also has the same format as of "if" statement.
Only additional thing is that an else part is added to if statement.
So if the condition satisfies the statements inside if part will be
executed else the statement inside else part will be executed
Syntax:
if(condition){
// set of statements if condition satisfies
}
else{
// set of statements if condition fails
}
Example Code:
<script language="javascript">
var a = "1234abc";
if(a == "adcdefa"){
document.write(" inside if statement ");
}else{
document.write(" inside else part of statement ");
}
</script>
Result:
In the above example the condition is to check if variable 'a' equals (==) "abcdefa".
The condition fails as we have assigned a as "1234abc". So the else part is executed.
|