An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
May 18, 2020 03:37 am GMT
Original Link: https://dev.to/codingunicorn/bang-bang-you-re-boolean-hoj
Bang bang, you're boolean!
If you see code like this:
function isLoggedIn() { return user !== undefined;}
You can refactor it to:
function isLoggedIn() { return !!user;}
A double negation !!
converts an object to boolean in accordance with type coercion rules. If the object is one of eight Falsy values defined in JavaScript specification (undefined
, null
, false
, NaN
, 0
, -0
, 0n
, ""
), the result will be false
. Otherwise, it will be true
.
Remember it by:
Bang, bang, you're boolean!
Original Link: https://dev.to/codingunicorn/bang-bang-you-re-boolean-hoj
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To