# not-defined

> checks if foo is not defined, i.e. undefined, null, an empty string, array, object or NaN

Latest version **3.0.7** (published 2025-12-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install not-defined
pnpm add not-defined
yarn add not-defined
bun add not-defined
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.0.7 |
| Published | 2025-12-17 |
| First published | 2016-02-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14 |
| Dependencies | 0 |
| Unpacked size | 4.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Gianluca Casati |
| Maintainers | fibo |
| Keywords | typeof, undefined, null, defined |

## Links

- npm: https://www.npmjs.com/package/not-defined
- Repository: https://github.com/fibo/not-defined
- Homepage: https://github.com/fibo/not-defined#readme
- Issues: https://github.com/fibo/not-defined/issues
- npm.io page: https://npm.io/package/not-defined

## Recent versions

- 3.0.7 (latest) — 2025-12-17
- 3.0.6 — 2025-08-06
- 3.0.5 — 2021-03-07
- 3.0.4 — 2021-02-10
- 3.0.3 — 2020-10-19
- 3.0.2 — 2020-10-16
- 3.0.1 — 2020-10-12
- 3.0.0 — 2020-06-28
- 2.1.4 — 2020-05-29
- 2.1.3 — 2020-04-16
- 2.1.2 — 2020-04-16
- 2.1.1 — 2020-04-16
- 2.1.0 — 2018-06-01
- 2.0.2 — 2018-04-17
- 2.0.1 — 2017-10-08
- … 5 more at https://npm.io/package/not-defined/versions

## README

# not-defined

> checks if foo is not defined, i.e. undefined, null, an empty string, array, object or NaN

## Installation

```shell
npm install not-defined
```

## Pros

* Type less.
* Better readability (even your boss will understand your code ^:).
* Can save bytes in your builds (not-defined module occupies only 171B).
* Easier to autocomplete in editors (for instance easier than `typeof foo === 'undefined'`).

## Usage

Signature is:

```ts
function notDefined(arg: any): boolean;
```

This snippet of code

```js
import notDefined from 'not-defined'

if (notDefined(foo)) {
  // do something, usually throw a TypeError
}
```

is equivalent to the following pseudocode

```
if (foo is not defined, i.e. is null, undefined, NaN, an empty string, array or object) {
  // do something, usually throw a TypeError
}
```

You can also use a shorter but still semantic form like

```js
import no from 'not-defined'

if (no(foo)) {
  // do something, usually throw a TypeError
}
```

A real use case: suppose you have a JSX that gets an array of `items` from an API.
If the array of `items` is empty, you want to render a message.

Instead of

```js
if (isLoading) {
  return <p>Loading...</p>
}

if (Array.isArray(items) && items.length === 0) {
  return <p>No items found</p>
}
```

you can write

```js
if (isLoading) {
  return <p>Loading...</p>
}

if (no(items)) {
  return <p>No items found</p>
}
```

Follows a list of [tested examples](https://github.com/fibo/not-defined/blob/main/test.js)

```js
no() // true
no(undefined) // true
no(null) // true
no('') // true
no([]) // true
no({}) // true
no(NaN) // true

no(0) // false
no(true) // false
no(false) // false
no('string') // false
no(['foo']) // false
no({ foo: true }) // false
no(42) // false
no(Infinity) // false
no(function () { return 1 }) // false
no(0n) // false
no(Symbol('foo')) // false
```

## License

[MIT](https://fibo.github.io/mit-license)

---
_Source: https://npm.io/package/not-defined · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
