if (this.meta.addText !== 'null')
{
console.log("hi");
}
因此,當this.meta.addText
爲空時,此檢查仍會進入if
並打印hi
。JavaScript空檢查 - 值爲空,但檢查仍然失敗?
我錯過了什麼?
if (this.meta.addText !== 'null')
{
console.log("hi");
}
因此,當this.meta.addText
爲空時,此檢查仍會進入if
並打印hi
。JavaScript空檢查 - 值爲空,但檢查仍然失敗?
我錯過了什麼?
因爲它是null
和而不是字符串'null'
。試試這個:
if (this.meta.addText !== null)
您正在測試字符串「null」,而不是針對null值。
沒有必要downvote這個。操作顯然只是困惑的JavaScript運營商,Antonpug我想你應該看看這裏瞭解更多關於JS:http://autotelicum.github.com/Smooth-CoffeeScript/literate/js-intro.html#operators – andlrc
這是你的實際的代碼?你真的想測試字符串「null」嗎?你的實際代碼是否測試了'null',但是你想測試'undefined'或'null OR undefined'? –