Šajā apmācībā jūs uzzināsit par JavaScript booleans ar piemēru palīdzību.
Programmā booleans ir primitīvi datu tipi, kas var būt truevai false. Piemēram,
const a = true; const b = false;
Piezīme . Ja jūs iesaiņojat truevai falsecitātā, tie tiek uzskatīti par virkni.
Piemēram,
const a = 'true'; console.log(typeof a); // string
Būla vērtības lielākoties tiek izmantotas salīdzināšanas un loģiskajiem operatoriem. Piemēram,
Vienāds ar operatoru ==atgriež, trueja operandi ir vienādi.
console.log(5 == 6); // false
Nav vienāds ar operatora !=atgriešanos, trueja visi operandi nav vienādi.
console.log(5 != 6); // true
Logical AND &&atgriež, trueja abas operanda vērtības ir true, cits novērtē ar false.
console.log(true && false); // false
Būla vērtības tiek izmantotas arī if… elsepaziņojumos un forcilpās.
Šeit ir saraksts ar vērtībām, kas tiek pārveidotas par noteiktām būla vērtībām.
| Datu tips | Būla vērtība |
|---|---|
| nenoteikts | nepatiesa |
| nulle | nepatiesa |
| NaN | nepatiesa |
| " | nepatiesa |
| 0 | nepatiesa |
| 20 | taisnība |
| -20 | taisnība |
| 'Sveiki' | taisnība |
JavaScript Būla metodes
Šeit ir JavaScript iebūvēto būla metožu saraksts.
| Metode | Apraksts |
|---|---|
toString() | atgriež būla vērtību, pārveidojot būla vērtību par virkni |
valueOf() | atgriež būla vērtības primitīvo vērtību |
Piemērs: toString () izmantošana
let count = false; // converting to string let result = count.toString(); console.log(result); console.log(typeof result);
Rezultāts
viltus virkne
Piemērs: valueOf () izmantošana
let count = true; // converting to string let result = count.valueOf(); console.log(result); console.log(typeof result);
Rezultāts
patiess Būla
JavaScript Būla () funkcija
Boolean()Funkcija tiek izmantota, lai pārvērstu dažādus datu tipus Būla vērtības. Piemēram,
const a = true; console.log(Boolean(a)); // true
Viss ar vērtību atgriežas true. Piemēram,
let result; result = 20; console.log(Boolean(result)); // true console.log(typeof Boolean(result)); // boolean result = -20; console.log(Boolean(result)); // true result = 'hello'; console.log(Boolean(result)); // true result = (a: 1); console.log(Boolean(result)); // true
JavaScript, undefined, null, 0 , NaN, ''pārveido to false. Piemēram,
let result; // empty string result = Boolean(''); console.log(result); // false result = Boolean(0); console.log(result); // false result = Boolean(undefined); console.log(result); // false result = Boolean(null); console.log(result); // false result = Boolean(NaN); console.log(result); // false
Piezīme . Ja vēlaties uzzināt vairāk par loģisko reklāmguvumu, apmeklējiet JavaScript tipa reklāmguvumu.
Būla objekti
Izmantojot newatslēgvārdu, varat izveidot arī būla vērtību . Piemēram,
const a = true; // creating a boolean object const b = new Boolean(true); console.log(a); // true console.log(b); // true console.log(typeof a); // "boolean" console.log(typeof b); // "object"
Piezīme . Ieteicams neizmantot būla objektus. Būla objektu izmantošana palēnina programmu.








