PHP isset() Equivalent in JavaScript

PHP's isset() checks both existence and non-null in one call; JavaScript's closest single-expression equivalent is `value != null` (loose inequality, which also excludes undefined), while optional chaining (`?.`) handles safe nested access.

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

PHP source
isset()
Closest JavaScript API
!= null (loose check) / optional chaining (?.)

Main limitation: PHP's `isset($var)` returns `false` for undefined variables and for variables explicitly set to `null`. JavaScript's `value != null` (using loose `!=`, not `!==`) evaluates to `false` for both `null` and `undefined` specifically because of a special case in the loose-equality spec — this is one of the few places where JavaScript's usually-confusing loose equality is exactly the tool you want.

How to use each function

Before comparing the two, here's how isset() and != null (loose check) / optional chaining (?.) work on their own.

PHP: isset()

isset(mixed $var, mixed ...$vars): bool

A language construct (not a real function) that checks whether one or more variables are set and not null in a single call, without triggering an undefined-variable warning. Passing multiple arguments returns true only if every one of them is set. It cannot distinguish a variable that was never defined from one explicitly set to null.

Returns: true only if every given variable/key exists and is not null.

PHP
$data = ["name" => "Ada"];
isset($data["name"]); // true
isset($data["age"]);  // false (key missing)

JavaScript: != null (loose check) / optional chaining (?.)

value != null / obj?.a?.b

JavaScript has no single function like isset(); value != null uses loose inequality specifically to catch both null and undefined in one comparison, while optional chaining (?.) safely short-circuits a nested lookup to undefined the moment any link in the chain is missing, without throwing.

Returns: A boolean (!= null) or the accessed value / undefined (?.).

JavaScript
const data = { age: null };
data.age != null;   // false
data?.a?.b;         // undefined, no error

Quick mapping

PHP isset()
Closest JavaScript API != null (loose check) / optional chaining (?.)

How it differs

  • PHP's isset($var) returns false for undefined variables and for variables explicitly set to null. JavaScript's value != null (using loose !=, not !==) evaluates to false for both null and undefined specifically because of a special case in the loose-equality spec — this is one of the few places where JavaScript's usually-confusing loose equality is exactly the tool you want.
  • For safely accessing a possibly-missing nested property without throwing, JavaScript's optional chaining (obj?.a?.b) is the idiomatic equivalent to PHP's isset($obj->a->b) short-circuiting behavior, and was specifically added to solve this class of problem.
  • The in operator in JavaScript ("key" in obj) checks key existence only, like Python's in — it returns true even if the value is null, so it does not replicate isset()'s combined behavior on its own.

Examples

Combined existence-and-non-null check

PHP
$data = ["age" => null];
isset($data["age"]); // false
JavaScript
const data = { age: null };
data.age != null; // false
JavaScript's loose `!= null` is a deliberate idiom that catches both `null` and `undefined` in one comparison, matching PHP's isset() behavior for this case.

Safe nested property access

PHP
isset($data["a"]["b"]); // false, no error, even if "a" is missing
JavaScript
data?.a?.b != null; // false, no error, even if "a" is missing
Optional chaining short-circuits to `undefined` at the first missing link, just like PHP's isset() does across a nested array/object path.

Edge cases

Edge case

Never use strict !== null alone to replicate isset() in JavaScript — it won't catch undefined. The idiomatic != null loose check is the one case where mixing null/undefined checking via loose equality is considered good practice, not an anti-pattern.

Edge case

Optional chaining (?.) returns undefined (not throwing) when any link in the chain is null/undefined, mirroring how isset() on a nested path never crashes even if an intermediate key is missing.

References

  • PHP Manual — isset — checked 2026-07-29
  • MDN Web Docs — Optional chaining (?.) — checked 2026-07-29

Last verified 2026-07-29.