JavaScript null check - 值为null,但检查仍然失败?

时间:2012-12-13 19:11:24

标签: javascript

if (this.meta.addText !== 'null')
{
    console.log("hi");
}

因此当this.meta.addText为空时,此检查仍会进入if并打印hi

我错过了什么?

3 个答案:

答案 0 :(得分:3)

'null'string

将其更改为null

var string1 = null,
    string2 = 'null';

console.log(string1 == string2); // false
console.log(null != 'null'); // true

你可以看到它here

答案 1 :(得分:2)

因为它是null,而不是字符串 'null'。试试这个:

if (this.meta.addText !== null)

答案 2 :(得分:1)

您正在测试字符串“null”,而不是针对值null。