Upon reviewing the current draft specification, it is evident that the behavior aligns with the specified guidelines.
The specification for Array.prototype.filter()
includes a step to test for the presence of a value before processing (step 8c):
- While
k < len
, repeat:
a. Define Pk
as ! ToString(k)
.
b. Check if kPresent
is true by querying HasProperty(O, Pk)
.
c. Proceed if kPresent
is true.
The specification for Array.prototype.find()
, on the other hand, does not include a similar test:
- While
k < len
, repeat:
a. Define Pk
as ! ToString(k)
.
b. Retrieve the value at Pk
using Get(O, Pk)
.
c. Continue with the process.
This discrepancy in behavior is clearly defined by the specifications themselves.
Regarding the design rationale behind this distinction, it may require further investigation. If you believe this difference constitutes a bug, submitting a report here would be appropriate.
Update
Evidence suggests that early drafts of the find()
and findIndex()
methods did involve checking and skipping array holes. These drafts were initially introduced during the ECMA Technical Committee 39 meeting on March 14th, 2013.
An argument was raised in August 2014 through a bug report suggesting that treating array holes as undefined
rather than skipping them would enhance consistency with prevailing standards from TC39.
Array#find
and Array#findIndex
should interpret holes as undefined
instead of bypassing them, in line with the prevalent trend within
TC39 to regard holes as undefined
.
This issue emerged during discussions about the intended behavior of Array.prototype.includes()
, particularly focusing on whether
[,,,].includes(undefined)
should yield true or false.
Furthermore, these conversations highlighted the shift within TC39 towards defining holes as undefined.
Consequently, hole checking for the find()
and findIndex()
methods was later omitted in revision 27 of the ECMAScript 6 Draft.
Exploring the linked references provides deeper insights into the decision to eliminate hole checking/skipping from the find()
and findIndex()
specifications, reflecting an overarching sentiment at the time that "holes: nobody wants them."