Booleans and comparisons · ブール値と比較
True and false
- A boolean is one of just two values:
trueorfalse. - Compare values to get a boolean:
===(equal),!==(not equal),<><=>=.
console.log(5 === 5); // → true
console.log(3 > 7); // → false
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.
console.log(true && false); // → false
console.log(!true); // → false
Truth table — try every input · 真値表 — すべての入力パターンを試してください
&& (and) is true only when both are true. || (or) is true when at least one is. · && (AND)は 両方 が真の場合にのみ真となる。|| (OR)は 少なくとも1つ が真の場合に真となる。
Watch out
- Always compare with
===, not==. The older==quietly converts types:0 == ""istrue! ===checks value and type, which is what you almost always want.
Common mistakes
- Prefer
===(strict) over==, which does surprising type coercion. &&is AND,||is OR,!is NOT.
Now you try
- Each task logs
trueorfalse. - Press Check answer to test it.
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 が偶数かどうかを n % 2 === 0 でテストしてログ出力せよ(それは 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. · 実行ボタンをクリックして出力を確認してください。