2014-05-01 22 views

回答

2

字符串"true""false"被認爲是真的。所以,千萬不要使用它們來代替布爾值。

console.log(Boolean("true")); 
# true 
console.log(Boolean("false")); 
# true 

console.log(true != "true"); 
# true 
console.log(false != "false"); 
# true 
1

你應該總是使用布爾值。
使用字符串"false"作爲布爾值仍然是truthy,因爲它不是空字符串。

Boolean("true") 
>> true 

Boolean("false") 
>> true 

Boolean("") 
>> false 
1

您應該使用布爾其中possible-其他原因比較布爾是遠遠快於字符串比較來執行。

相關問題