0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScriptで配列がnull/undefinedではないが空であることの判定法

Last updated at Posted at 2025-03-08

概要

JavaScriptで、配列がnullやundefinedではないが、空であることを示す関数です。

定義

function isEmptyArrayButNotNullish(arr){
    return typeof arr !== "undefined" && arr !== null && Array.isArray(arr) && arr.length === 0; 
}

呼び出し例

const x1 = isEmptyArrayButNotNullish(null);
const x2 = isEmptyArrayButNotNullish(undefined);
const x3 = isEmptyArrayButNotNullish([]);
const x4 = isEmptyArrayButNotNullish([1, 2]);
console.log(x1,x2,x3,x4);

表示結果

false false true false
0
0
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?