Not True vs False

    if (userRecord === false) { // didn't work
      console.log("inside if userRecord === false");
      models.UserAccount.create({
        username : req.body.username,
        password : hashedPassword
      })

    if (!userRecord) { // worked
      console.log("inside if !userRecord");
      models.UserAccount.create({
        username : req.body.username,
        password : hashedPassword
      })

Above code uses Sequelize and tests if a record was found. I assumed === false would be true if Sequelize didn’t return anything, but it never triggers, only !userRecord works.

I know the ! operator is good for flipping values like on a checkbox. But why is “Not true” here different from False?

userRecord was probably some other falsy value (like null or undefined), not exactly the value false. That’s why the === comparison fails.

1 Like

Hmm, I just tried this

if (userRecord == false)

still doesn’tt work.

Maybe it comes back as {}. I think that is truthy

Hmmm, if you’re getting {} then the second if shouldn’t work. Does the sequelize docs say anything about what it returns if no records are found?

I just logged userRecord, it comes back as null. Strange. I thought that was falsy

As far as I know null can only == with another null and undefined. Not with false.

1 Like

PG/MySQL/etc use null for nonexistence, so I assume Sequelize takes that and converts it to a JS null. false is a perfectly acceptable value in a DB column (it would have a boolean type), whereas null value for a field is (with respect to RDBMs that Sequalize deals with) special. I assume that field will never be false, because that means the [whatever SQL implementation] type of the relevant column would be boolean; you’re checking if the lookup returns null, not whether it is false

(Edit: which all makes using ! slightly dangerous in some regards but hey it’s JS :man_shrugging:)

1 Like