Skip to content

Function: isEmpty()

ts
function isEmpty(value): boolean;

Checks if a value is empty according to JavaScript's notion of emptiness. 根据JavaScript的空值概念检查值是否为空。

A value is considered empty if it is:

  • undefined or null
  • An empty string, array, or array-like object
  • An empty Map or Set object
  • An object with no own enumerable properties

以下情况被认为是空值:

  • undefinednull
  • 空字符串、数组或类数组对象
  • 空的Map或Set对象
  • 没有自有可枚举属性的对象

Parameters

ParameterTypeDescription
valueanyThe value to check / 要检查的值

Returns

boolean

True if the value is empty / 如果值为空则返回true

Example

typescript
// Primitive empty values / 原始空值
isEmpty(undefined) // => true
isEmpty(null) // => true
isEmpty('') // => true
isEmpty(0) // => false (zero is not empty)
isEmpty(false) // => false (false is not empty)

// Collections / 集合
isEmpty([]) // => true
isEmpty([1, 2, 3]) // => false
isEmpty({}) // => true
isEmpty({ key: 'value' }) // => false

// Built-in objects / 内置对象
isEmpty(new Map()) // => true
isEmpty(new Map([['key', 'value']])) // => false
isEmpty(new Set()) // => true
isEmpty(new Set([1, 2, 3])) // => false

// Array-like objects / 类数组对象
isEmpty(arguments) // => true (if arguments is empty)
isEmpty({ length: 0 }) // => true
isEmpty({ length: 1, 0: 'item' }) // => false

Since

1.0.0

Released under the MIT License.