-1

I know this might be a noob questions, but when I handle error for mongodb, why don't we put a condition like this in inside the if statement?

For example

query.findOne(function (err, kitten) {
    if (err === true) {
        console.log(err);
    } else if (!err) {
        console.log("success");
    }
}

How is if(err) equivalent to if(err===true)?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Eric
  • 77
  • 9

1 Answers1

1

If err is a boolean variable, and is set to True, then you do not need to write if(err===True) because it is the same as writing if(True) (because err is true). In contrast, if you want to check if the condition is False, then you write if(!err). If err is True, then !err= !True = False

LoukasPap
  • 1,244
  • 1
  • 8
  • 17
  • This does answer my question, but I have something that I want to dig in. What if err is not an boolean value, for example when I use the mongoose query. I am not sure what kind of variable is the err, but I don't think its simply just a boolean variable since when I console log it, it shows more. And for this case, how would I understand this? – Eric Aug 30 '20 at 08:28
  • What does it show you, when you print it on console? – LoukasPap Aug 30 '20 at 09:15