Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js 空值判断

作者:望断天涯   发布日期:2026-06-20   浏览:71

// 判断变量是否为空值(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 判断是不是空对象

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js fill方法

js 数组连接

js json数组

js 数组复制

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站