John Au-Yeung
Seuraa
5. Elokuu 2019 · 2 min lukea
Se on helppo muuntaa mitään boolen JavaScript. Totuudenmukaiset arvot muunnetaan oikeiksi ja horjuvat arvot vääriksi. On 2 tapoja muuntaa muuttujia boolean., 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.,
Jos sinulla arvioida niitä boolean-kuten niin, saat:
toisin kuin truthy arvoja, kuten esineitä, jotka ovat jotain muuta, mutta arvojen edellä:
Boolean({}) // true
Boolean(true) // true
Boolean() // true
Voit myös käyttää !! pikakuvake valettu Boolean, oikea ! valettu muuttujan osaksi boolean ja tyhjäksi arvo, ja vasen ! kumoa se takaisin todellinen totuusarvo.,
!!(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
