JavaScript 中的 if...else if... 语句是什么?
if...else if... 语句是 if...else 的高级形式,它允许 JavaScript 根据多个条件制定正确的决策。
语法
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
}示例
您可以尝试运行以下内容,了解如何在 JavaScript 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>
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP