Booleans and comparisons · 布尔值与比较
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
True and false
- A boolean is one of just two values:
trueorfalse. - Compare values to get a boolean:
===(equal),!==(not equal),<><=>=.
中文
真与假
- 布尔值(boolean)只有两个取值之一:
true或false。 - 比较两个值就能得到布尔值:
===(等于)、!==(不等于)、<><=>=。
console.log(5 === 5); // → true
console.log(3 > 7); // → false
English
Combine tests
&&means and — true only when both sides are true.||means or — true when at least one side is true.!means not — it flipstrueandfalse.
中文
组合多个测试
&&表示与 —— 只有两边都为真时才为真。||表示或 —— 只要至少一边为真就为真。!表示非 —— 把true和false互换。
console.log(true && false); // → false
console.log(!true); // → false
Explore · 探索
Truth table — try every input · 真值表 —— 试试每种输入
&& (and) is true only when both · 两者 are true. || (or) is true when at least one is. · &&(与)只有两边都为真时才为真。||(或)只要至少一边为真就为真。
English
Watch out
- Always compare with
===, not==. The older==quietly converts types:0 == ""istrue! ===checks value and type, which is what you almost always want.
中文
注意
- 永远用
===比较,不要用==。老式的==会悄悄转换类型:0 == ""居然是true! ===同时检查值和类型,这几乎总是你想要的。
English
Common mistakes
- Prefer
===(strict) over==, which does surprising type coercion. &&is AND,||is OR,!is NOT.
中文
常见错误
- 优先用
===(严格相等),==会做出人意料的类型转换。 &&是与,||是或,!是非。
English
Now you try
- Each task logs
trueorfalse. - Press Check answer to test it.
中文
现在轮到你
- 每个任务打印
true或false。 - 按检查答案来测试它。
Use === to log whether 10 equals 10 (it is true). · 用 === 打印 10 是否等于 10(是 true)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Log whether 8 is greater than 3 (it is true). · 打印 8 是否大于 3(是 true)。
Click Run to see the output here. · 点击“运行”查看此处输出。
let n = 8. Log whether n is even by testing n % 2 === 0 (it is true). · let n = 8。通过测试 n % 2 === 0 打印 n 是否为偶数(是 true)。
Click Run to see the output here. · 点击“运行”查看此处输出。
let age = 15. Use && to log whether the age is from 13 to · 到 19 (a teenager) — it is true. · let age = 15。用 && 打印这个年龄是否在 13 到 19 之间(青少年)—— 是 true。
Click Run to see the output here. · 点击“运行”查看此处输出。