条件
一句话解释
条件是 表达式那个 evaluates 到真 or 假, determining 哪个分支的 code 执行.
用大白话说
A condition is a question your code asks that has a yes/no (true/false) answer. Based on the answer, the program decides what to do next. For example, "是用户 logged in?" is a condition — if yes, show the dashboard; if no, show the login page.
Conditions use comparison operators like === (equal), > (greater than), < (less than) and logical operators like && (AND), || (OR), ! (NOT) to build expressions that evaluate to booleans.
The 如果/else statement is the most common way to use conditions. The 如果 block runs when the condition is true; the else block runs when it is false. You can chain multiple conditions with else 如果.
示例
let temperature = 25;
if (temperature > 30) {
console.log("It's hot!");
} else if (temperature > 20) {
console.log("It's warm.");
} else {
console.log("It's cool.");
} 条件 temperature > 30 是假, 但 temperature > 20 是真, 所以 "It's warm." 是 printed.
常见混淆
容易混淆的对象: 循环
关键区别: 条件是 evaluated 一次到 制作 decision; 循环条件是 evaluated repeatedly 到 control iteration. 条件分支; 循环 repeat.
相关术语
相关资源
相关课程
- 条件
学习如何条件工作.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。