While
[]
equalsfalse
, it evaluates totrue
.
yes, this sounds bad or at least a bit confusing. Take a look at this:
const arr = [];if (arr) console.log("[] is truethy");if (arr == false) console.log("however, [] == false");
In practice, if you want to check if something is empty,then check the length
. (The ?.
operator makes sure that also null
is covered.)
const arr = []; // or null;if (!arr?.length) console.log("empty or null")