Reimplement all JavaScript Array Functions with while loops only
Last month, out of curiosity, I reimplemented all 38 JavaScript Array functions
(e.g. .forEach()
, .map()
, .sort()
, etc.) with while loops only. As always,
I learned something as I work on the all the functions. Some features, edge
cases, considerations that I previously do not know about JavaScript.
- There are sparse arrays and the pain of handling them
- Arrays are objects and its implications
- What copy of a reference in JavaScript actually means
- What are iterators and generators
- Different kinds of equality in JavaScript
Other than that, there are some less-known functions that I discovered, at least
I have not used them before until now, like .copyWithin()
, .splice()
and
.with()
.
To be honest, most of the implementation is tedious and repetitive. A lot of
effort goes into handling edge cases, which are the interesting things I
learned. The implementation is not as interesting compare to random things I
learned, so I will focus on sharing the lessons first. But if you really want
to, you can skip to the
implementation considerations
where I explained the setup, how some functions are implemented, especially some
interesting one like .flat()
and .sort()
.
The full code is available in this repo.