How to correctly determine whether an array is empty

Determine whether the array is empty - the wrong way

const arr = []

if (arr) {
  console.log('true')
} else {
  console.log('false')
}

// true

为什么空数组为 true 呢?
Here Insert Picture Description
We found an empty array deployed inside or have a default property


Determine whether the array is empty - the correct way

if (arr && arr.length > 0) {
  console.log('true')
}

Determine whether an array is empty, it must determine thelength > 0

Published 23 original articles · won praise 0 · Views 554

Guess you like

Origin blog.csdn.net/JIANLI0123/article/details/104684724