我想弄清楚为什么我的if语句正在发送真正的输出,尽管在我的if语句中为null情况。有人可以解释我的条件和空触发原因为什么是错的吗?Javascript - 如果语句返回空尽管条件
if(startDate || startDate != null){
filterQuery.push('dateStart=' + startDate);
}
在存在空值的情况下输出dateStart=null
。
我想弄清楚为什么我的if语句正在发送真正的输出,尽管在我的if语句中为null情况。有人可以解释我的条件和空触发原因为什么是错的吗?Javascript - 如果语句返回空尽管条件
if(startDate || startDate != null){
filterQuery.push('dateStart=' + startDate);
}
在存在空值的情况下输出dateStart=null
。
使用
if(typeof startDate !== "undefined") {
这与null值有什么关系?这实际上确保将打印出'null',这是OP不需要的。 – trincot
你们可能想检查这一个http://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in –
'typeof startDate;'返回什么? –
如果'startDate'确实是'null',那么这个条件肯定是错误的。 (如果'startDate'是undefined,那么它也是错误的。) –
它返回字符串 – cphill