0

I'm trying to figure out why my if statement is sending true output's despite accounting for null situations in my if statement. Can someone explain what is wrong with my conditions and the reason why null triggers for true?

if(startDate || startDate != null){
            filterQuery.push('dateStart=' + startDate);
}

outputs dateStart=null in instances where there is a null value.

cphill
  • 5,596
  • 16
  • 89
  • 182

1 Answers1

2

You've said that typeof startDate gives you "string". That's your problem: "null" is a truthy value (so it passes the first test) that is != null (so it passes the second test). So your condition is true and the output concatenate the string "null" to your error message.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Great call. I should have defaulted to using `typeof` before jumping to the assumption that it was an actual `null` value – cphill Feb 19 '17 at 19:15