如何在 JavaScript 中编写内联 IF 语句?
条件语句是任何编程语言中最重要和最基本的概念。if-else 语句允许我们有条件地执行任何代码块。我们可以在大括号中定义 if 语句的条件,如果条件为真,则执行 if 代码块;否则,执行 else 代码块。
在这里,我们演示了 if-else 语句在 JavaScript 中是如何工作的。
if (condition) {
// code to execute when the condition becomes true
} else {
// code to execute when the condition becomes false
}
从上面的代码中,用户可以学习 if-else 语句的语法。
如果我说你可以将上面五行代码写成一行,你会怎么想?是的,你可以使用内联 if 语句来做到这一点。
语法
用户可以按照以下语法在 JavaScript 中使用内联 if 语句。
Condition? code - block - 1 : code - block - 2
在上面的语法中,condition 是一个表达式。当 condition 表达式计算结果为真时,它执行 code block 1;否则,它执行 code block 2。
如果我们将内联 if 语句与 if-else 语句进行比较,code-block-1 是 if 语句的代码,code-block-2 是 else 语句的代码。
示例
在下面的示例中,我们将学习内联 if 语句的基本用法。我们使用了条件 '10===10',如果条件计算结果为真,它将打印 '10 等于 10';否则,它将打印 '10 不等于 10'。
在输出中,用户可以看到它打印了 '10 等于 10',因为条件始终计算结果为真。
<html>
<body>
<h2>Using the <i> inline if statement </i> in JavaScript</h2>
<div id = "output"> </div>
<script>
let output = document.getElementById('output');
let result = 10 === 10 ? "10 is equal to 10." : "10 is not equal to 10.";
output.innerHTML += "The value of the result variable: " + result;
</script>
</body>
</html>
示例
在下面的示例中,我们创建了数字数组。此外,我们还创建了 func1() 和 func2() 函数,它们打印带有作为参数传递的值的不同消息。
我们使用 forEach() 方法遍历数组。在 forEach() 方法的回调函数中,我们检查数字是否可以被 10 整除,如果是,则调用 func1() 函数;否则,调用 func2() 函数。
<html>
<body>
<h2>Using the <i> inline if statement </i> in JavaScript</h2>
<div id = "output"> </div>
<script>
let output = document.getElementById('output');
function func1(value) {
output.innerHTML += "The func1() is executed for the value " + value + "<br>";
}
function func2(value) {
output.innerHTML += "The func2() is executed for the value " + value + "<br>";
}
let numbers = [10, 30, 43, 50, 64, 76, 87];
numbers.forEach((num) => {
num % 10 == 0 ? func1(num) : func2(num);
})
</script>
</body>
</html>
示例
在下面的示例中,我们使用 if-else 语句和内联 if 语句检查年份是否为闰年。checkYear() 函数使用 if-else 语句来确保作为参数传递的年份是否为闰年。
在 checkInlineYear() 函数中,我们实现了与 checkYear() 函数相同的逻辑,但是我们将 if-else 语句转换为内联 if 语句。用户可以看到我们如何在一行中编写了九行代码。
用户可以观察到这两个函数对于任何年份值都给出相同的输出。
<html>
<body>
<h3>Using inline if statement to check whether year is leap year in JavaScript</h3>
<div id = "output"> </div>
<script>
let output = document.getElementById('output');
function checkYear(year) {
// if the year is divisible by 400, it is a leap year.
if (year % 400 == 0) {
return true;
// if the year is divisible by 400 and by 100, it is not a leap year.
} else if (year % 100 == 0) {
return false;
// if the year is divisible by 400, not divisible by 100, and divisible by 4, it is a leap year.
} else if (year % 4 == 0) {
return true;
} else {
return false;
}
}
function checkInlineYear(year) {
return year % 400 == 0 ? true : year % 100 == 0 ? false : year % 4 == 0 ? true : false;
}
output.innerHTML += "Outputs using the checkYear() function. <br> ";
output.innerHTML += "The 2023 is leap year :- " + checkYear(2020) + "<br>";
output.innerHTML += "The 3000 is leap year :- " + checkYear(3000) + "<br>";
output.innerHTML += "<br>";
output.innerHTML += "Outputs using the checkInlineYear() function. <br> ";
output.innerHTML += "The 2023 is leap year :- " + checkInlineYear(2020) + "<br>";
output.innerHTML += "The 3000 is leap year :- " + checkInlineYear(3000) + "<br>";
</script>
</body>
</html>
用户学习了如何在 JavaScript 中使用内联 if 语句。我们可以观察到,内联 if 语句使代码更清晰易读,并且始终建议使用更少的代码行来实现相同的逻辑。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP