【发布时间】:2011-02-08 00:53:20
【问题描述】:
给定以下代码:
if ("string") {
console.log('true!');
}
//logs "true" to the console
if ("string"==true) {
console.log('true!');
}
//doesn't log anything
为什么会这样?我认为"string" 被转换为一个数字,布尔值也是如此。所以true 变成1,"string" 变成NaN。第二个 if 语句是有道理的,但我不明白为什么第一个语句会导致内部循环被评估。这是怎么回事?
【问题讨论】:
标签: javascript