jiku
jiku

Reputation: 283

Conditional based on _.where

With

var a = _.where(list, {key: "value"});
if (a) console.log("exists");

the conditional doesn't seem to work, but if I change to if (a != "") it does. Since a = [] when not finding anything I'm using if (a.length > 0), but just curious why it works with an empty string and then not with the original example. Isn't an empty string in JS a falsy?

Upvotes: 0

Views: 204

Answers (1)

Ram
Ram

Reputation: 144689

Yes, an empty string is a falsy value in JavaScript, but where method returns an array and an array is an object which is not a falsy value, even empty arrays are truthy.

[] != '' // false
!![] // true

Upvotes: 1

Related Questions