1. 判断NaN
// NaN出现的原因:(Not a Number)
// 操作两个类型不一致的数、用NaN值计算最终得到NaN、不合法运算(如0 / 0)
let num = 0/0; if (isNaN(num)) { console.log("num为NaN!") }
2. 判断undefined
let temp = undefined // 方法一 if (typeof(temp) == "undefined") { console.log("temp is undefined") } // 方法二 if (tmp == undefined) { console.log("temp is undefined") }
// typeof()返回8种可能的结果:number, string, boolean, object, function, undefined, bigint, symbol
3. 判断null
let temp = null; if (temp === null) { console.log("temp is null") }
4. 判断undefined和null
let temp = undefined // 方法一:== undefined if (temp == undefined) { console.log("temp may be null or undefined") } // 方法二:== null if (temp == null) { console.log("temp may be null or undefined") }
5. 同时判断null, undefined, 0, NaN, false, 空字符串
let temp = null; if (!temp) { console.log("null or undefined or NaN or 0 or false or 空字符串") }标签:false,undefined,temp,JavaScript,NaN,console,null,log From: https://www.cnblogs.com/gamepen/p/17730741.html