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 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.
A Note
Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script.
Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.