以下是几种情况:
if(null) {console.log("123")} else {console.log("2222")}//2222
if(undefined) {console.log("123")} else {console.log("2222")}//2222
if(0) {console.log("123")} else {console.log("2222")}//2222
if(-0) {console.log("123")} else {console.log("2222")}//2222
if(NaN) {console.log("123")} else {console.log("2222")}//2222
if("") {console.log("123")} else {console.log("2222")}//2222
if(false) {console.log("123")} else {console.log("2222")}//2222
if(1) {console.log("123")} else {console.log("2222")}//123
if(" ") {console.log("123")} else {console.log("2222")}//123
示例代码中,前七个,对于给定条件的求值都是false
追根溯源,是因为布尔值:所有不具有“真实”值的即为 false
0、-0、null、undefined、NaN、""、false
以上七种都不具有真实值。