If Else Statement
What is the use of if-else statement?
How to use if else method in javascript?
Explanation
If else statement also has the same format as that 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:
<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.