è facile da convertire in qualcosa di un valore booleano in JavaScript. I valori Truthy verranno convertiti in true e i valori falsy verranno convertiti in false. Ci sono 2 modi per convertire le variabili in un booleano., There is the Boolean function, and there is the !! shortcut.
There are 6 kinds of falsy values:
false0- empty string:
"",'', or`` nullundefined-
NaN— not a number value
Anything else is truthy.,
Se si valutano as boolean piace, in questo modo, si ottiene:
contrariamente a truthy valori come oggetti, che sono niente altro, ma i valori di cui sopra:
Boolean({}) // true
Boolean(true) // true
Boolean() // true
È anche possibile utilizzare il tag !! collegamento al cast di Boolean, il diritto ! cast variabile in un booleano e negare il valore, e la sinistra ! negarlo al vero valore booleano.,
!!(false); // false
!!(0); // false
!!(""); // false
!!(''); // false
!!(``); // false
!!(undefined); // false
!!(null); // false
!!(NaN); // false
Similarly for truthy values:
!!({}) // true
!!(true) // true
!!() // true
With shortcut evaluation, boolean ANDs and ORs are evaluated up to the first truthy value:
const abc = false && "abc"; // false, since false is false and "abc" is trueconst def = false || "def"; // true, since false is false and "def" is true
