if...else if... 문은 JavaScript가 여러 조건에서 올바른 결정을 내릴 수 있도록 하는 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 작업 방법을 배울 수 있습니다. JavaScript의 문 -
라이브 데모
<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>