รูปแบบทั่วไปของการตรวจสอบแบบย่อใน JavaScript

รูปแบบการตรวจสอบแบบย่อยังทำงานได้ สำหรับประเภทข้อมูลอื่น ๆ ในกรณีนี้ ข้อมูลเหล่านั้นจะถูกแปลงเป็น ประเภทบูลีนก่อน แล้วจึงนำไปเปรียบเทียบ กับ true ดูตัวอย่าง:

javascriptlet test = 3;

if (test) {
  console.log('+++'); // จะทำงานส่วนนี้
} else {
  console.log('---');
}

จริง ๆ แล้วโค้ดที่นำเสนอนั้น เทียบเท่ากับสิ่งต่อไปนี้:

javascriptlet test = 3;

if (Boolean(test) === true) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 1

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test = 3;

if (test) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 2

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test = 'abc';

if (test) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 3

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test = '';

if (test) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 4

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test = 3 * 'abc';

if (test) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 5

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test = null;

if (test) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 6

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test = false;

if (test) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 7

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test;

if (test) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 8

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test = 0;

if (test) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 9

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test = '0';

if (test) {
  console.log('+++');
} else {
  console.log('---');
}
Задание 10

โดยไม่ต้องรันโค้ด จงกำหนดว่าอะไรจะถูกแสดงผล ในคอนโซล:

javascriptlet test = -1;

if (test) {
  console.log('+++');
} else {
  console.log('---');
}