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.
$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.
["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'sincludes(), which always uses the SameValueZero algorithm (like===but treatingNaNas equal to itself); always compare against PHP's strict mode (in_array($needle, $haystack, true)) when translating. - JavaScript's
includes()correctly findsNaNin an array ([NaN].includes(NaN)istrue), whereasArray.prototype.indexOf()cannot; PHP'sin_array()has no direct concept ofNANsearch since PHP'sNANalso 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'sin_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
in_array("b", ["a", "b", "c"]); // true ["a", "b", "c"].includes("b"); // true Strict comparison to avoid type coercion
in_array("1", [0, 1, 2], true); // false (strict) [0, 1, 2].includes("1"); // false 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.