很多时候,开发者想要不同的结果,这取决于正在发生的行动。为了这个目的,JavaScript为我们提供了条件语句。
以下是JavaScript中的条件语句。
- 如果(包含特定的代码,如果提到的条件为真,则需要执行)。
- else (包含特定的代码,如果提到的条件是假的,就需要执行)
- else if (包含一个新的条件,如果提到的第一个条件是假的)
- switch (包含需要执行的各种代码块)
让我们通过例子来更好地理解每一个语句。
if 语句
JavaScript中的if语句包含一个特定的代码,如果提到的条件为真,就需要执行。请记住,写If或IF而不是if会在JavaScript中产生一个错误。
语法
if (condition) {
// execute the if block if the condition is true}
这里,代码将进入if块的情况被定义在条件的位置。
例子
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display "Good Morning", only if the time is less than 12 PM.</p>
<button onclick="Display()">Display</button>
<p id="div"></p>
<script>
function Display() {
var time = new Date().getHours();
if (time <12) {
document.getElementById("div").innerHTML = "Good Morning";
}
}
</script>
</body>
</html>
在这个例子中,只有当时间小于12点时才会被执行。否则,将没有输出。
输出

你也可以执行多个if条件,其中每个if条件都将被检查,但是,最好使用if-else条件,而不是一次使用多个if条件。
if else 语句
使用单一的if语句大多没有好处,因为如果if块中的条件不为真,则代码执行时没有任何输出。为此,我们使用if...else条件,它与if一起使用,以确保如果if块中的条件为假,则执行else块。
语法
if (condition) {
// execute the if block if the condition is true
}
else {
// execute the else block if the condition of if block is false
}
在这里,除了if,我们还有else,这表明如果条件没有得到满足,就执行代码的else部分。
例子
<!DOCTYPE html>
<html>
<body>
<p>Click the button to check if the number is less than 5 or greater than 5.</p>
<button onclick="Display()">Check</button>
<p id="div"></p>
<script>
function Display() {
var x = 7;
if (x <5) {
document.getElementById("div").innerHTML = "Number Less than 5";
}
else {
document.getElementById("div").innerHTML = "Number Greater than 5";
}
}
</script>
</body>
</html>
这里,else条件被执行,因为if块内的条件不为真。
输出

else if 语句
有时,用户希望在第一个条件为假的情况下检查各种条件,为此需要使用else if条件。其主要优点是,我们可以根据程序的流程,使用else if检查多个条件。
语法
if (condition1) {
// execute the if block if the condition is true}
else if(condition2){
// execute the elseif block if the condition of if block is false true
}
else {
// execute the else block if the condition all above are false
}
在这里,除了if else,我们还有第三个代码块else if,它包含了另一个需要检查的条件,而且一段代码可以有多个else if块。
例子
<!DOCTYPE html>
<html>
<body>
<p>Click the button to check if the number is less than, equal to or greater than 5.</p>
<button onclick="Display()">Check</button>
<p id="div"></p>
<script>
function Display() {
var x = 5;
if (x <5) {
document.getElementById("div").innerHTML = "Number Less than 5";
}
else if(x == 5){
document.getElementById("div").innerHTML = "Number is equal to 5";
}
else {
document.getElementById("div").innerHTML = "Number Greater than 5";
}
}
</script>
</body>
</html>
这里,程序检查了条件并执行了else if部分,因为提到的数字等于5。
输出

此外,我们还可以在条件语句中使用比较以及逻辑运算符,将一个以上的条件作为一个单一的条件进行检查,更确切地说。
- 比较运算符(, ===, !==, >=, <=)
- 逻辑运算符(&&检查两条语句,这两条语句都需要为真,代码才会被执行。而||检查两条语句,其中只有一条是真的,代码才会被执行)
例子
<!DOCTYPE html>
<html>
<body>
<p>Click the button to check which range the number falls into.</p>
<button onclick="Display()">Check</button>
<p id="div"></p>
<script>
function Display() {
var x = 13;
if (x >0 && x<=5) {
document.getElementById("div").innerHTML = "Number between range of 0-5";
}
else if(x>5 && x<= 10){
document.getElementById("div").innerHTML = "Number between range of 5-10";
}
else if(x>10 && x<= 15){
document.getElementById("div").innerHTML = "Number between range of 10-15";
}
else {
document.getElementById("div").innerHTML = "Number Greater than 15 ";
}
}
</script>
</body>
</html>
输出

在上面的例子中,用户使用逻辑运算符检查各种条件。程序检查了提到的两个条件,如果两个条件都是真的,就执行代码。
开关语句
为了执行各种不能用else if检查的语句,因为它看起来不专业,那么这些语句最好用一个switch语句来执行。在switch中,我们只是简单地将案例分配给每个代码,可以是数字或字符串。
语法
switch(statement) {
case a:
// code
break;
case b:
// code
break;
default:
// code
}
语句被检查一次,每个案例都被对照检查。如果没有匹配的情况,那么就执行默认的代码块。
例子
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript switch Conditions</h2>
<p id="div"></p>
<script>
let day;
switch (new Date().getDay()) {
case 0:
day = "Sunday.";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday. Weekend almost here";
break;
case 6:
day = "Finally it's Saturday";
}
document.getElementById("div").innerHTML = "Today is " + day;
</script>
</body>
</html>
输出

在这里,语句与每个案例进行了检查,当它与案例相匹配时,就会产生输出。
总结
在这篇文章中,我们了解到条件是如何在编程中发挥重要作用的。Javascript为我们提供了各种条件语句,帮助我们根据需要做出的决定来维持程序的流程。
通过它,你可以更有逻辑地组织你的程序,并控制它以获得更好的性能。这样一来,程序对其他人来说也变得容易理解。