JavaScript in 2016: the equality contract behind Array.includes
Array.prototype.includes became part of ECMAScript 2016. It is more than a shorter indexOf expression: examples with NaN, sparse arrays and object identity show the comparison contract.
Archive retrospective · Topic year 2016
In this article
The contract behind a shorter expression
Ecma’s General Assembly approved the seventh edition of the ECMAScript language specification on June 14, 2016. Array.prototype.includes belongs to that edition. This article was prepared in December 2026 as a retrospective, keeping the historical feature separate from the actual publication date. The everyday question is whether an array contains a value. Includes returns a Boolean directly, replacing many familiar comparisons of indexOf with minus one. The expressions do not behave identically for every input, however. We therefore define accepted inputs before checking equality at the boundaries. The example concerns small-scale data validation, rather than authorization or a complete state-transition policy.
Make the accepted input type explicit
The example accepts a delivery state only when it is a string equal to pending or sent. The type check is our function’s contract, rather than an additional validation service provided by includes. A number, undefined or an object is rejected. Uppercase SENT is rejected too. If a product needs case-insensitive input, normalization should be a separate decision. Converting every value to a string could introduce unwanted representations of null or objects. We avoid that conversion. The four expected results are true, false, false and false, and were checked locally. Membership alone does not establish whether a transition from one valid state to another is permitted.
const states = ['pending', 'sent'];
function accepts(raw) {
return typeof raw === 'string' && states.includes(raw);
}
console.log(accepts('sent'));
console.log(accepts('SENT'));
console.log(accepts(1));
console.log(accepts(undefined));
// Output: true, false, false, false (one per line)
Why NaN reveals a difference
Includes uses the specification’s SameValueZero comparison. Searching for NaN in an array containing NaN returns true, whereas indexOf returns minus one. That difference makes the feature more than a shorter spelling. Finding NaN still does not establish that it represents a valid measurement. Imagine an import process using NaN for unsuccessful numeric conversions: detecting such a value and accepting it as a measurement would be different operations. The data-quality rule needs its own definition. Positive and negative zero also match for membership. If a domain needs to distinguish those representations, includes cannot substitute for that more specific requirement. Equality belongs in the data contract.
A missing slot has a separate history
Array(1) creates an array whose length is one but whose first position has no actual element. Includes can read that position as undefined, while indexOf skips the absent slot. The demonstrated results therefore differ. This does not establish that the application explicitly stored undefined there. In a hypothetical import, a missing field, an intentionally empty value and failed conversion could represent distinct problems. One membership query would not explain their origins. A production validator would also need to consider how the array was constructed. Checking length alone does not establish that every position contains an element, just as a column count does not establish that every field is populated.
console.log([NaN].includes(NaN));
console.log([NaN].indexOf(NaN));
console.log(Array(1).includes(undefined));
console.log(Array(1).indexOf(undefined));
const item = { id: 7 };
console.log([item].includes(item));
console.log([item].includes({ id: 7 }));
// Output: true, -1, true, -1, true, false (one per line)
Object identity differs from matching fields
The final checks search for the same object reference and then a newly created object with the same id field. The first returns true and the second false. Includes does not compare every field for structural equality. If a selection screen defines the same order by its id, that business rule needs a separate expression. A field comparison with some could be appropriate, but the identifier’s type and uniqueness would then become part of the contract. Serializing objects to compare their text introduces other questions. The point is to identify what equality means before choosing a method, particularly when a refresh creates new objects representing existing records.
What the local check establishes
We ran both snippets in local Node.js and verified all ten expected outputs. That confirms the described behavior in the current runtime used for the check. It does not prove support in every 2016 browser. Standard approval and implementation in a particular product are separate historical facts. Maintenance of an older application would require checking its target environment and any transformation layer. No package installation, real user data or speed benchmark was needed here. Before replacing an indexOf expression, ask whether the requirement concerns the same value, the same object or the same business identifier. The original diagram separates input validation from membership for precisely that reason. Maintenance also requires reading the callers. One screen might display an explicit invalid-state message while another silently substitutes a default. Identical membership results would not establish identical user behavior. Empty strings and numeric-looking strings should be checked against the chosen contract too; neither is in our allowed list.
Reader discussion
Which bottleneck would you expect as this solution scales?
No comments yet. Be the first to contribute.
Write a comment