PHP in_array() Equivalent in JavaScript

JavaScript's Array.prototype.includes() is the closest equivalent to PHP's in_array() in strict mode — both compare by value and type, though the exact algorithms (SameValueZero vs. strict equality) differ at the edges.

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

PHP source
in_array()
Closest JavaScript API
Array.prototype.includes()

Main limitation: PHP's loose-mode `in_array()` (the default) performs type-juggling comparisons that have no equivalent in JavaScript's `includes()`, which always uses the SameValueZero algorithm (like `===` but treating `NaN` as equal to itself); always compare against PHP's strict mode (`in_array($needle, $haystack, true)`) when translating.

How to use each function

Before comparing the two, here's how in_array() and Array.prototype.includes() work on their own.

PHP: in_array()

in_array(mixed $needle, array $haystack, bool $strict = false): bool

Checks whether a value exists anywhere in an array. By default it uses loose (==) comparison, which can produce surprising type-coercion matches; passing true as the third argument forces strict (===) comparison.

Returns: true if the value is found, false otherwise.

PHP
$roles = ["admin", "editor", "viewer"];
in_array("editor", $roles); // true
in_array("0", [0, 1, 2], true); // false (strict)

JavaScript: Array.prototype.includes()

arr.includes(searchElement, fromIndex)

Tests whether searchElement appears in the array using the SameValueZero algorithm (like === but treating NaN as equal to itself). This makes it the closest match to PHP's strict in_array($needle, $haystack, true), not PHP's default loose mode, which has no JavaScript equivalent.

Returns: true if found, false otherwise.

JavaScript
["admin", "editor", "viewer"].includes("editor"); // true

Quick mapping

PHP in_array()
Closest JavaScript API Array.prototype.includes()

How it differs

  • PHP's loose-mode in_array() (the default) performs type-juggling comparisons that have no equivalent in JavaScript's includes(), which always uses the SameValueZero algorithm (like === but treating NaN as equal to itself); always compare against PHP's strict mode (in_array($needle, $haystack, true)) when translating.
  • JavaScript's includes() correctly finds NaN in an array ([NaN].includes(NaN) is true), whereas Array.prototype.indexOf() cannot; PHP's in_array() has no direct concept of NAN search since PHP's NAN also fails == comparisons — this is one of the few cases with no clean parallel.
  • For objects/arrays as elements, JavaScript's includes() compares by reference, not by deep equality; PHP's in_array() on nested arrays compares by value — deeply-equal-but-distinct objects will differ in JS but may match in PHP.

Examples

Basic membership check

PHP
in_array("b", ["a", "b", "c"]); // true
JavaScript
["a", "b", "c"].includes("b"); // true
A direct, exact translation for same-type values.

Strict comparison to avoid type coercion

PHP
in_array("1", [0, 1, 2], true); // false (strict)
JavaScript
[0, 1, 2].includes("1"); // false
PHP's strict third argument is required to match JavaScript's `includes()`, which never coerces types during comparison.

Edge cases

Edge case

Use in_array($needle, $haystack, true) (strict) as the PHP baseline when your JavaScript code uses includes(), since JS has no loose/type-juggling comparison mode to match against.

References

  • PHP Manual — in_array — checked 2026-07-29
  • MDN Web Docs — Array.prototype.includes() — checked 2026-07-29

Last verified 2026-07-29.