2012-12-13 86 views
-1
if (this.meta.addText !== 'null') 
{ 
    console.log("hi"); 
} 

因此,當this.meta.addText爲空時,此檢查仍會進入if並打印hiJavaScript空檢查 - 值爲空,但檢查仍然失敗?

我錯過了什麼?

+0

沒有必要downvote這個。操作顯然只是困惑的JavaScript運營商,Antonpug我想你應該看看這裏瞭解更多關於JS:http://autotelicum.github.com/Smooth-CoffeeScript/literate/js-intro.html#operators – andlrc

+0

這是你的實際的代碼?你真的想測試字符串「null」嗎?你的實際代碼是否測試了'null',但是你想測試'undefined'或'null OR undefined'? –

回答

3

'null'string

將其更改爲null

var string1 = null, 
    string2 = 'null'; 

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

你可以看到它here

+0

儘量使用===和!==比較器來避免隱式類型轉換。 http://oreilly.com/javascript/excerpts/javascript-good-parts/bad-parts.html – nicoabie

2

因爲它是null而不是字符串'null'。試試這個:

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

您正在測試字符串「null」,而不是針對null值。