Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→util-nonemptynext

util-nonempty5.2.3

Is the input (plain object, array, string or whatever) not empty?

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • PLAIN RECORDS
  • RECURSIVE ALT…
  • API — NONEMPTY()
  • API — VERSION
  • Changelog

No dependencies whatsoever. This package declares no dependencies or devDependencies.

Permalink to InstallationInstallation

Permalink to Quick TakeQuick Take

Permalink to ExamplesExamples

  • Check whether an array contains a value
  • Treat a deeply nested array as non-empty
  • Treat every number, including zero and NaN, as non-empty
  • Check whether an object contains a property
  • Treat nullish and Boolean values as empty

Purpose

nonEmpty() reports whether one supported value is shallowly non-empty. It checks only the outer string, array, or record; it never traverses nested values and does not mutate the input.

InputResultRule
Omitted, undefined, or null
Omitted, undefined, or nullfalseThese values are empty.
Primitive string
Primitive stringtrue when length > 0Whitespace is content.
Native array
Native arraytrue when length > 0Sparse slots count; expando properties do not.
Plain record
Plain recordtrue when it has an own enumerable string keySymbol, non-enumerable, and inherited keys do not count.
Primitive number
Primitive numbertrueThis includes zero, NaN, and both infinities.
Any other value
Any other valuefalseThis includes booleans, bigints, symbols, functions, collections, boxed primitives, and class instances.

Nested contents do not affect the result. For example, [[]] is non-empty because its outer array has one slot. A zero-length array remains empty even if it has an enumerable string property.

nonEmpty();
// => false

nonEmpty(" \n\t");
// => true

nonEmpty(new Array(1));
// => true

nonEmpty({ value: undefined });
// => true

nonEmpty(new Map([["value", true]]));
// => false

Plain records

Supported records include object literals, values created by new Object(), direct Object.create(null) records, and ordinary records from another realm. Custom-prototype objects and class instances are not plain records.

Record non-emptiness follows Object.keys() semantics. Only own, enumerable, string-keyed properties count. The function does not read ordinary property values or invoke their getters while discovering keys.

Recursive alternatives

Use ast-is-empty when you need to traverse nested arrays and plain objects and treat only zero-length strings as empty. Whitespace is content under that strict definition.

Use ast-contains-only-empty-space when nested strings containing only spaces, tabs, or line breaks should also count as empty.

API — nonEmpty()

The main function nonEmpty() is imported like this:

The function accepts one optional input. Omitting it is equivalent to passing undefined:

It returns a boolean according to the shallow rules above.

API — version

You can import version:

Permalink to changelogChangelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab