window.onload=function(){
if(test()&&true){
console.log("hello");
}
console.log(test()&&true);
};
function test(){
}
为什么 console.log(true&&undefined) 返回 undefined 而 if(true&&undefined) 返回 false?
window.onload=function(){
if(test()&&true){
console.log("hello");
}
console.log(test()&&true);
};
function test(){
}
为什么 console.log(true&&undefined) 返回 undefined 而 if(true&&undefined) 返回 false?
if(true&&undefined)不返回任何东西。-AND表达式的 true&&undefined计算结果undefined与参数一样好console.log,并且由于undefined是一个虚假值,因此if 语句的主体将不会被执行。
使用&&运算符时,返回第一个 false 值。
由于undefined是假的,因此该if语句被评估为假。