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

js 判断数组是否为空

作者:飞吻狂魔   发布日期:2026-06-22   浏览:56

// 判断数组是否为空的示例代码

// 方法1: 使用 length 属性
function isArrayEmpty(arr) {
    return arr.length === 0;
}

// 示例
let emptyArray = [];
let nonEmptyArray = [1, 2, 3];

console.log(isArrayEmpty(emptyArray)); // 输出: true
console.log(isArrayEmpty(nonEmptyArray)); // 输出: false

// 方法2: 使用 Array.isArray 和 length 属性结合,确保传入的是数组
function isArrayEmptySafe(input) {
    return Array.isArray(input) && input.length === 0;
}

// 示例
console.log(isArrayEmptySafe(emptyArray)); // 输出: true
console.log(isArrayEmptySafe(nonEmptyArray)); // 输出: false
console.log(isArrayEmptySafe("not an array")); // 输出: false

上一篇:js ascii码转字符

下一篇:js 判断数组不为空

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js fill方法

js 数组连接

js json数组

js 数组复制

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

Laravel 中文站