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.
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.
!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, andfalse— notably, JavaScript's"0"string is truthy (same divergence as Python), and JavaScript addsNaNto its falsy set where PHP does not treatNANas empty. - PHP's
empty()safely handles undefined variables and missing array keys without error; JavaScript's!variablethrows aReferenceErrorfor a genuinely undeclared variable (though!obj.missingKeyis safe and evaluates totrue, since accessing a missing object property yieldsundefined, 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 === 0for arrays/strings orObject.keys(obj).length === 0for plain objects instead.
Examples
Basic empty check (scalars)
empty(0); // true
empty(null); // true
empty(""); // true !0; // true
!null; // true
!""; // true Empty array: PHP falsy, JavaScript truthy
empty([]); // true ![].length; // true (must check .length, not the array itself)
![]; // false — the array object itself is truthy! 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.