LoginSignup
1
1

More than 5 years have passed since last update.

Javascriptの配列の空を判定する

Last updated at Posted at 2018-10-11

Javascriptの配列が空であることを判定する記述です。


var arr = [];
if (arr.length === 0) {
  console.log('配列は空です');
}

空のオブジェクトの場合はobj.lengthではundefinedとなりますので、以下の記述が必要です。


var obj = {};
console.log(obj.length);  // undefined
if (Object.keys(obj).length === 0) {
  console.log('空のオブジェクトです');
}
1
1
1

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
1
1