// 判断变量是否为空值(null 或 undefined)
function isEmpty(value) {
return value === null || value === undefined;
}
// 示例用法
let testVar1 = null;
let testVar2 = undefined;
let testVar3 = '';
console.log(isEmpty(testVar1)); // 输出: true
console.log(isEmpty(testVar2)); // 输出: true
console.log(isEmpty(testVar3)); // 输出: false
// 解释说明:
// 1. `isEmpty` 函数用于检查传入的值是否为 null 或 undefined。
// 2. 使用严格的相等运算符 `===` 来确保只匹配这两种空值,而不会误判其他类型的 "空" 值,例如空字符串或数字 0。
上一篇:js 判断对象不为空
下一篇:js 判断是不是空对象
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站