ifステートメント内の条件は、JavaScript に対して、trueまたはJavaScript内でどのように評価さfalseれますか?
2 に答える
3
undefined、null、""、0、NaN、およびfalseはすべて「誤った」値です。それ以外はすべて「真の」値です。
「falsey」値をテストすると、条件は false になります。例えば:
var a = 0;
if (a) {
// Doesn't happen
}
else {
// Does happen
}
「真の」値をテストすると、条件は真になります。
var a = 1;
if (a) {
// Does happen
}
else {
// Doesn't happen
}
于 2013-01-26T11:32:29.433 に答える
3
Whatever the result of the condition expression is, it is converted to a Boolean by ToBoolean:
- Let
exprRefbe the result of evaluatingExpression.- If
ToBoolean(GetValue(exprRef))istrue, then
Return the result of evaluating the firstStatement.- Else,
Return the result of evaluating the secondStatement.
For each data type or value, the conversion rules to a Boolean are clearly defined. So, for example, undefined and null are both converted to false.
于 2013-01-26T11:41:36.420 に答える