PHP empty() Equivalent in JavaScript

PHP's empty() is a null-safe truthiness check; JavaScript's `!value` covers the same intent, but — like Python — treats the string "0" as truthy where PHP treats it as empty.

Requires review Works only under specific conditions — read the caveats before relying on it.

PHP source
empty()
Closest JavaScript API
! (truthiness check)

Main limitation: PHP's `empty()` falsy set includes `""`, `"0"`, `0`, `0.0`, `null`, `false`, and `[]`. JavaScript's falsy set is `""`, `0`, `-0`, `0n` (BigInt zero), `NaN`, `null`, `undefined`, and `false` — notably, JavaScript's `"0"` string is truthy (same divergence as Python), and JavaScript adds `NaN` to its falsy set where PHP does not treat `NAN` as empty.

How to use each function

Before comparing the two, here's how empty() and ! (truthiness check) work on their own.

PHP: empty()

empty(mixed $var): bool

A language construct that safely tests truthiness without ever warning on an undefined variable or missing array key. It returns true for a fixed set of "empty" values: '', '0' (the string zero), 0, 0.0, null, false, and []. Effectively shorthand for !isset($var) || !$var.

Returns: true if the variable is unset or holds one of PHP's empty values, false otherwise.

PHP
empty([]);   // true
empty("0");  // true — PHP-specific quirk
empty("hi"); // false

JavaScript: ! (truthiness check)

!value

Negates JavaScript's own truthiness test. Falsy values are '', 0, -0, 0n, NaN, null, undefined, and false — notably arrays and objects are always truthy regardless of contents, so ![] is false even though PHP's empty([]) is true. Check .length === 0 for arrays instead.

Returns: true if value is falsy, false otherwise.

JavaScript
!0;    // true
![];   // false — an empty array is a truthy object

Quick mapping

PHP empty()
Closest JavaScript API ! (truthiness check)

How it differs

  • PHP's empty() falsy set includes "", "0", 0, 0.0, null, false, and []. JavaScript's falsy set is "", 0, -0, 0n (BigInt zero), NaN, null, undefined, and false — notably, JavaScript's "0" string is truthy (same divergence as Python), and JavaScript adds NaN to its falsy set where PHP does not treat NAN as empty.
  • PHP's empty() safely handles undefined variables and missing array keys without error; JavaScript's !variable throws a ReferenceError for a genuinely undeclared variable (though !obj.missingKey is safe and evaluates to true, since accessing a missing object property yields undefined, which is falsy).
  • Unlike PHP, JavaScript's empty *array* ([]) and empty *object* ({}) are NOT falsy — both are truthy in JavaScript, because all objects (including arrays) are truthy regardless of contents. Check .length === 0 for arrays/strings or Object.keys(obj).length === 0 for plain objects instead.

Examples

Basic empty check (scalars)

PHP
empty(0); // true
empty(null); // true
empty(""); // true
JavaScript
!0; // true
!null; // true
!""; // true
Scalar falsy values match closely between PHP and JavaScript for these common cases.

Empty array: PHP falsy, JavaScript truthy

PHP
empty([]); // true
JavaScript
![].length; // true (must check .length, not the array itself)
![]; // false — the array object itself is truthy!
This is the critical divergence: PHP's empty() considers an empty array empty, but JavaScript arrays are always truthy as objects — you must explicitly check `.length === 0`.

Edge cases

Edge case

The empty-array/object difference is the biggest trap here: empty([]) is true in PHP, but ![] is false in JavaScript (an empty array is a truthy object). Always use array.length === 0 to check for an empty JS array, never bare truthiness.

References

  • PHP Manual — empty — checked 2026-07-29
  • MDN Web Docs — Truthy — checked 2026-07-29

Last verified 2026-07-29.