🔰how array methods deal with "holes" ?

JSobjectbuilt-inArraysparse array ⟩ how array methods deal with "holes" ?

loops and array methods treat "holes" in sparse arrays differently.

  • for-oftreats "holes" as undefined.

  • concat — keeps holes

  • copyWithin — holes are copied

  • entries, keys, values — treats holes as undefined

  • every — ignores holes

  • fill — fills holes

  • filterremoves holes (its result is always a "dense" array). 👉 arr.filter()

  • find — treats holes as elements

  • findIndex — treats holes as elements

  • flat - removes holes but keeps undefined.

  • forEachignores holes (closure not invoked). 👉 for-of vs. forEach

  • indexOf — ignores holes

  • join — converts holes to empty strings

  • lastIndexOf — ignores holes

  • mappreserves holes (but ignored by mapping function)

  • pop — treat holes as elements

  • push — preserves holes

  • reduce , reduceRight— ignores holes

  • reverse — preserves holes

  • shift — treat holes as undefined

  • slice — preserves holes

  • sort — preserves holes

  • toString — preserves holes

  • unshift — preserves holes

  • values — converts holes to undefined

Last updated