I want to understand Array.prototype.find() method

this challenge could be solved with the array.prototype.find method and I am reading the documentation but I am getting stuck at this part which says

callbackFn is invoked for every index of the array, not just those with assigned values. This means it may be less efficient for sparse arrays, compared to methods that only visit assigned values.

what does it mean by callback function not just those with assigned values what are those assigned values

Challenge: Finders Keepers

Link to the challenge:

Sparse arrays are arrays with “holes” in them, i.e. [1, 2,,4,,6]. What the docs is saying is that the find() callback is invoked no matter if the element has an assigned value or not.

Trailing commas in literals

If more than one trailing comma is used, an elision (or hole) is produced. An array with holes is called sparse (a dense array has no holes). When iterating arrays for example with Array.prototype.forEach() or Array.prototype.map(), array holes are skipped.

There is an example in the forEach docs of how it has no operation for uninitialized values

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.