Booleans and comparisons
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ 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),<><=>=.

English
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.
English
console.log(true && false); // → false
console.log(!true); // → false
สำรวจ
Truth table — try every input
&& (and) is true only when ทั้งสองอย่าง 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.
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.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ใช้ === to log whether 10 equals 10 (it is true).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Log whether 8 is greater than 3 (it is true).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
let n = 8. Log whether n is even by testing n % 2 === 0 (it is true).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
let age = 15. Use && to log whether the age is from 13 ไป 19 (a teenager) — it is true.
คลิก Run เพื่อดูผลลัพธ์ที่นี่