微信备份wordpress,排名优化课程,整合营销传播的效果表现为,泉州西街js判断是否为null#xff0c;undefined#xff0c;NaN#xff0c;空串或者空对象 这里写目录标题 js判断是否为null#xff0c;undefined#xff0c;NaN#xff0c;空串或者空对象特殊值nullundefinedNaN空字符串#xff08;#xff09;空对象#xff08;…js判断是否为nullundefinedNaN空串或者空对象 这里写目录标题 js判断是否为nullundefinedNaN空串或者空对象特殊值nullundefinedNaN空字符串空对象{} typeof函数判断undefined判断nullundefined和null 和 的理解 判断NaN判断空字符串判断对象为空使用JSON.stringify()使用es6的方法Object.keys()for in 循环判断使用Object对象的getOwnPropertyNames方法 判断空数组 特殊值
null
是一个特殊的值表示一个空或者不存在的对象引用表示一个变量被人为的设置为空对象不是原始状态。
undefined
当一个变量被声明但没有赋值时它的默认值是 undefined。此外当访问不存在的对象属性或数组元素时结果也是 undefined。
NaN
Not a Number是一个特殊的值表示在执行数学运算时结果不是一个合法的数字。NaN 属于 Number 类型它与任何值包括 NaN 本身都不相等。
出现NaN的情况
数学运算中的无效操作例如0 / 0 或 Math.sqrt(-1)将非数字的值强制转换为数字时例如parseInt(abc) 或 Number(xyz)
空字符串“”
一个空字符串是一个长度为 0 的字符串。它不是 null 或 undefined而是一个完全有效的字符串只是它没有内容。
空对象{}
空对象是一个没有任何属性的对象。它不是 null、undefined 或空字符串而是一个有效的 JavaScript 对象。
typeof函数 基本类型String、Number、Boolean、Symbol、Undefined、Null 引用类型Objectobject、array、function typeof // string
typeof Bill Gates // string
typeof 0 // number
typeof 3.14 // number
typeof false // boolean
typeof true // boolean
typeof Symbol() // symbol
typeof Symbol(mySymbol); // symbol
typeof undefined // undefined
typeof null // object
typeof {name:Bill, age:62} // object
typeof [0, 1, 2] // object
typeof function(){} // function
typeof ((){}) // function
typeof testnull // function testnull是函数名判断undefined
let a undefined;
if (typeof (a) undefined || a undefined) {console.log(undefined);
}判断null
需要注意的是 typeof(null) object
let b null;
console.log(typeof (b));if (b null) {console.log(null1);
}
// !key typeof(key)!undefined 过滤完之后只剩 null 和 0 了再用一个 key!0 就可以把 0 过滤掉了
if (!b typeof (b) ! undefined b ! 0) {console.log(null2);
}undefined和null
if(a null) { // 等同于 a undefined || a nullconsole.log(为null);
}undefined 和 null 用 比较是相等的
null 的类型是 objectundefined 的类型是 undefined 是先把左右两边转化为相同的类型再进行区分 和 的理解 严格相等会先比较类型再比较值 宽松相等是先把左右两边转化为相同的类型再进行区分仅要求值相等而不要求类型 true 转化为整数后是 1false 转化为整数后是 0 https://cloud.tencent.com/developer/article/1703891 https://blog.csdn.net/qq_42533666/article/details/129190944
判断NaN
NaN具有非自反的特点所谓的非自反就是说NaN 与谁都不相等包括它本身但在 NaN ! NaN 下会返回true可以根据这个特性判断
let num NaN;
let num2 0/0;if (typeof (num) number isNaN(num)) {console.log(num is NaN1);
}
if (num2 ! num2) {console.log(num is NaN2);
}https://blog.csdn.net/Lu5957/article/details/122609879 https://blog.csdn.net/qq_43563538/article/details/103815482 https://www.cnblogs.com/k1023/p/11851943.html
判断空字符串
直接将其与空字符串比较或者调用 trim() 函数去掉前后的空格然后判断字符串的长度
判断字符串为空空格算空
let str ;if (str || str.trim().length 0){console.log(empty str 1);
}if (typeof str string str.length 0) {console.log(not empty str);
}判断字符串不为空空格不算空
if (typeof s string s.length 0) {console.log(not empty str);
}https://blog.csdn.net/K346K346/article/details/113182838
判断对象为空
使用JSON.stringify()
将 JavaScript 值转换为 JSON 字符串再判断该字符串是否为{}
let data {};
if (JSON.stringify(data) {}) {console.log(empty obj 1);
}使用es6的方法Object.keys()
if (Object.keys(data).length 0) {console.log(empty obj 2);
}for in 循环判断
通过 for…in 语句遍历变量的属性
if (judge(data)) {console.log(empty obj 3);
}
function judge(data) {for (let key in data) {return false;}return true;
}使用Object对象的getOwnPropertyNames方法
使用Object对象的getOwnPropertyNames方法获取到对象中的属性名存到一个数组中返回数组对象通过判断数组的length来判断此对象是否为空
if (Object.getOwnPropertyNames(data).length 0) {console.log(empty obj 4);
}https://www.jb51.net/article/209387.htm https://www.cnblogs.com/shanhubei/p/16969190.html https://blog.csdn.net/qq_38709383/article/details/123224833
判断空数组
首先判断变量是否为数组然后通过 length 属性确定
let arr [];
if (arr.length 0) {console.log(empty arr 1);
}if (arr instanceof Array !arr.length) {console.log(empty arr 2);
}if (Array.isArray(arr) !arr.length) {console.log(empty arr 3);
}