รูปแบบทั่วไปของการตรวจสอบแบบย่อใน JavaScript
รูปแบบการตรวจสอบแบบย่อยังทำงานได้
สำหรับประเภทข้อมูลอื่น ๆ ในกรณีนี้
ข้อมูลเหล่านั้นจะถูกแปลงเป็น
ประเภทบูลีนก่อน แล้วจึงนำไปเปรียบเทียบ
กับ true ดูตัวอย่าง:
let test = 3;
if (test) {
console.log('+++'); // จะทำงานส่วนนี้
} else {
console.log('---');
}
จริง ๆ แล้วโค้ดที่นำเสนอนั้น เทียบเท่ากับสิ่งต่อไปนี้:
let test = 3;
if (Boolean(test) === true) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test = 3;
if (test) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test = 'abc';
if (test) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test = '';
if (test) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test = 3 * 'abc';
if (test) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test = null;
if (test) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test = false;
if (test) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test;
if (test) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test = 0;
if (test) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test = '0';
if (test) {
console.log('+++');
} else {
console.log('---');
}
โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:
let test = -1;
if (test) {
console.log('+++');
} else {
console.log('---');
}