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

js 判断为空

作者:佳凝皓月   发布日期:2025-11-15   浏览:50

// 判断变量是否为空的几种常见方式

// 1. 使用 typeof 判断 undefined
function isEmpty(value) {
    return typeof value === 'undefined';
}

// 2. 使用 null 和 undefined 判断
function isNullorUndefined(value) {
    return value == null; // 这里会同时判断 null 和 undefined
}

// 3. 判断空字符串
function isEmptyString(value) {
    return value === '';
}

// 4. 综合判断 (null, undefined, 空字符串, NaN)
function isBlank(value) {
    return value == null || value === '' || isNaN(value);
}

// 示例用法
console.log(isEmpty(undefined)); // true
console.log(isNullorUndefined(null)); // true
console.log(isEmptyString("")); // true
console.log(isBlank(NaN)); // true
console.log(isBlank("")); // true
console.log(isBlank(null)); // true
console.log(isBlank(undefined)); // true
console.log(isBlank("hello")); // false

解释说明:

  • isEmpty 函数用于判断变量是否为 undefined
  • isNullorUndefined 函数用于判断变量是否为 nullundefined,使用 == null 可以同时匹配这两种情况。
  • isEmptyString 函数用于判断变量是否为空字符串 ""
  • isBlank 函数是一个综合判断函数,可以同时判断变量是否为 nullundefined、空字符串或 NaN

这些方法可以帮助你在 JavaScript 中有效地判断变量是否为空。

上一篇:js 判断字符串不为空

下一篇:js 判断对象不为空

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象数组合并

js 对象转数组

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

Laravel 中文站