การแปลงค่าเป็นประเภทบูลีนใน JavaScript

ตอนนี้เรามาศึกษาการแปลงประเภทข้อมูลอื่นๆ เป็นบูลีนกัน การแปลงแบบนี้ ทำได้โดยใช้ฟังก์ชัน Boolean นี่คือค่าต่างๆ ที่เมื่อแปลงเป็นประเภทบูลีนแล้ว จะได้ false:

javascriptconsole.log(Boolean(0));          // false
console.log(Boolean(-0));         // false
console.log(Boolean(+0));         // false
console.log(Boolean(null));       // false
console.log(Boolean(false));      // false
console.log(Boolean(NaN));        // false
console.log(Boolean(undefined));  // false
console.log(Boolean(''));         // false

ค่าอื่นๆ ทั้งหมดใน JavaScript (ในภาษาโปรแกรมอื่น อาจไม่เป็นเช่นนี้) จะให้ true นี่คือ ค่าบางค่าที่แปลงเป็น true แต่ อาจทำให้คุณสับสนได้:

javascriptconsole.log(Boolean(-1));          // true
console.log(Boolean(Infinity));    // true
console.log(Boolean(-Infinity));   // true

ค่าต่อไปนี้เป็นสตริง เนื่องจาก อยู่ในเครื่องหมายคำพูด และจะแปลงเป็น true เช่นกัน:

javascriptconsole.log(Boolean('0'));         // true
console.log(Boolean('false'));     // true
console.log(Boolean('NaN'));       // true
console.log(Boolean('null'));      // true
console.log(Boolean('undefined')); // true
Задание 1

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(3);
alert(test);
Задание 2

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(0);
alert(test);
Задание 3

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(-1);
alert(test);
Задание 4

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(-0);
alert(test);
Задание 5

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(+0);
alert(test);
Задание 6

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean('abc');
alert(test);
Задание 7

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean('');
alert(test);
Задание 8

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean('0');
alert(test);
Задание 9

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(true);
alert(test);
Задание 10

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(false);
alert(test);
Задание 11

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean('true');
alert(test);
Задание 12

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean('false');
alert(test);
Задание 13

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(null);
alert(test);
Задание 14

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean('null');
alert(test);
Задание 15

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(undefined);
alert(test);
Задание 16

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean('undefined');
alert(test);
Задание 17

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(NaN);
alert(test);
Задание 18

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean('NaN');
alert(test);
Задание 19

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(3 * 'abc');
alert(test);
Задание 20

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(Infinity);
alert(test);
Задание 21

โดยไม่ต้องรันโค้ด จงระบุว่าอะไรจะแสดงผล บนหน้าจอ:

javascriptlet test = Boolean(1 / 0);
alert(test);