cphill
cphill

Reputation: 5914

Javascript - If Statement Returning Null Despite Condition

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.

Upvotes: 0

Views: 56

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1073968

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.

Upvotes: 2

Related Questions