if...else if... स्टेटमेंट if…else का एक उन्नत रूप है जो जावास्क्रिप्ट को कई स्थितियों में से एक सही निर्णय लेने की अनुमति देता है।
सिंटैक्स
if-else-if स्टेटमेंट का सिंटैक्स इस प्रकार है -
if (expression 1){
Statement(s) to be executed if expression 1 is true
}
else if (expression2){
Statement(s) to be executed if expression 2 is true
}
else if (expression3){
Statement(s) to be executed if expression 3 is true
}
else{
Statement(s) to be executed if no expression is true
} उदाहरण
आप if…else if के साथ काम करने का तरीका जानने के लिए निम्नलिखित को चलाने का प्रयास कर सकते हैं जावास्क्रिप्ट में स्टेटमेंट -
लाइव डेमो
<html>
<body>
<script>
var book= "maths";
if( book== "history" ){
document.write("<b>History Book</b>");
}
else if(book == "maths" ){
document.write("<b>Maths Book</b>");
}
else if(book == "economics" ){
document.write("<b>EconomicsBook</b>");
}
else{
document.write("<b>Unknown Book</b>");
}
</script>
</body>
<html>