Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ความสัมพันธ์ระหว่าง 'null' และ '0' ใน JavaScript

การเปรียบเทียบ null และ 0 ให้ผลลัพธ์น่าสนใจ: null > 0 → false, null < 0 → false, null == 0 → false แต่ null >= 0 → true

ตัวอย่าง ตรวจสอบ >, <, ==

<html> <body> <script> 
    if(null > 0){ document.write("null is greater than 0"); } 
    else if(null < 0) { document.write("null is less than 0"); } 
    else if(null == 0){ document.write("null is equal to 0"); } 
    else { document.write("It is a typical relationship"); } 
</script> </body> </html>

ผลลัพธ์

It is a typical relationship

ตัวอย่าง ตรวจสอบ >=

<html> <body> <script> 
    if(null > 0){ document.write("null is greater than 0"); } 
    else if(null == 0){ document.write("null is equal to 0"); } 
    else if(null >= 0) { document.write("null is greater than or equal to 0"); } 
</script> </body> </html>

ผลลัพธ์

null is greater than or equal to 0

พฤติกรรมนี้แตกต่างจากคณิตศาสตร์ทั่วไป เป็นกรณีพิเศษของ JavaScript