JavaScript equivalent of PHP's in_array()

JavaScript equivalent of PHP's in_array()

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

JavaScript Equivalent of PHP's in_array()

Developing in a new language often involves translating mental models from your previous stack. If you are coming from PHP, the concept of checking for an element's existence within a collection is straightforward thanks to in_array(). This function returns a boolean indicating whether a specified value exists in an array, with optional flags to ignore case or compare only keys. In JavaScript, there isn't a single built-in utility function that mirrors this exact behavior out of the box, but the solution relies on a set of well-understood array methods and type coercion rules.

The most direct translation involves using Array.prototype.includes(). Introduced in ES2016, this method was designed specifically to fill the gap left by the lack of a native "in" operator for arrays. The syntax is clean and readable: myArray.includes(value). It returns true if the value is present, even as an exact element within the array, and false otherwise.

const fruits = ['apple', 'banana', 'cherry'];

// Returns true
fruits.includes('banana'); // true

// Returns false
fruits.includes('date'); // false

However, relying solely on includes() without understanding its type coercion behavior can lead to subtle bugs. The method performs loose equality checks (==) rather than strict equality (===). This means it will consider an empty string and a missing value as equivalent, or treat the number 0 as equivalent to false.

const mixed = [1, 2, '', false];

mixed.includes(0); // true (because 0 == false)
mixed.includes(''); // true (because '' == '')
mixed.includes(null); // false (null is not equal to anything in loose comparison except undefined/empty string)

This behavior often surprises developers transitioning from strict typing languages or those used to PHP's default integer-based comparisons. If you need strict type checking—where 0 must not match false, and an empty string must not match a missing value—you should filter the array first or use a custom comparison function.

// Strict check without using includes directly
mixed.some(item => item === 0); // true (matches the number)
mixed.some(item => item === false); // true (matches the boolean)

For more complex scenarios, such as checking if an object exists within an array of objects based on a specific property, includes() falls short. It checks for reference equality or primitive value matching, not deep structural comparison. To find an object where id matches a specific value, you must use .find() combined with a callback:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

// Returns the first matching object or undefined
users.find(user => user.id === 2); // { id: 2, name: 'Bob' }

// To get a boolean similar to in_array return type
const exists = users.some(user => user.id === 2); // true

When performance becomes a concern, specifically dealing with large datasets where you perform frequent lookups, iterating over the array every time is inefficient. In PHP, in_array() can be optimized by converting the search array to an associative map if keys are needed. The JavaScript equivalent is often to create a Set from the collection before checking for existence.

const largeCollection = new Set([1, 2, 3, 4, 5]);

// O(1) lookup time vs O(n) for includes on large arrays
largeCollection.has(3); // true

Using a Set is particularly useful when you are dealing with primitive values and need to avoid the overhead of linear search. However, keep in mind that Set behaves differently than a standard array regarding duplicates and type handling; it only accepts one value for each key. If your data involves objects or primitives where type coercion matters, stick with .some() or .includes().

For developers looking to handle these patterns across many projects, Laravel provides robust collection helpers that abstract away much of this boilerplate logic. The LaravelCompany platform offers tools and resources for building scalable applications where such utility methods are often pre-implemented in Eloquent or Collection classes, allowing you to focus on business logic rather than raw array manipulation.

Our network