Booleans and comparisons · Boolean และการเปรียบเทียบ
True and false
- A boolean is one of just two values:
trueorfalse. - Compare values to get a boolean:
===(equal),!==(not equal),<><=>=.
True และ false
- Boolean มีค่าเพียงสองค่า:
trueหรือfalse - เปรียบเทียบค่าเพื่อรับ Boolean:
===(เท่ากัน),!==(ไม่เท่ากัน),<><=>=
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.
รวมเงื่อนไขทดสอบ
&&หมายถึง and - เป็นจริงก็ต่อเมื่อทั้งสองฝั่งเป็นจริง||หมายถึง or - เป็นจริงเมื่ออย่างน้อยหนึ่งฝั่งเป็นจริง!หมายถึง not - มันสลับtrueและfalse
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) เป็นจริงเมื่อ อย่างน้อยหนึ่ง เป็นจริง.
Watch out
- Always compare with
===, not==. The older==quietly converts types:0 == ""istrue! ===checks value and type, which is what you almost always want.
ระวัง
- เปรียบเทียบเสมอด้วย
===, ไม่ใช่==ตัวเก่า==จะแปลง Type อย่างเงียบๆ:0 == ""คือtrue! ===ตรวจสอบทั้ง Value และ Type, ซึ่งเป็นสิ่งที่คุณต้องการแทบทุกครั้ง
Common mistakes
- Prefer
===(strict) over==, which does surprising type coercion. &&is AND,||is OR,!is NOT.
ข้อผิดพลาดที่พบบ่อย
- เลือกใช้
===(Strict) แทน==ซึ่งมีการแปลง Type ที่น่าประหลาดใจ &&คือ AND,||คือ OR,!คือ NOT
Now you try
- Each task logs
trueorfalse. - Press Check answer to test it.
ลองดูเลย
- ทุกงานจะบันทึกผลลัพธ์
trueหรือfalse - กด Check answer เพื่อทดสอบ
Use === to log whether 10 equals 10 (it is true). · ใช้ === เพื่อตรวจสอบว่า 10 เท่ากับ 10 หรือไม่ (ผลลัพธ์คือ true)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Log whether 8 is greater than 3 (it is true). · Log ว่า 8 มากกว่า 3 หรือไม่ (คือ true).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
let n = 8. Log whether n is even by testing n % 2 === 0 (it is true). · let n = 8. Log ว่า n เป็นคู่หรือไม่โดยทดสอบ n % 2 === 0 (คือ true).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่