Skip to main content

One post tagged with "JavaScript"

View All Tags

Reimplement all JavaScript Array Functions with while loops only

· 33 min read

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.

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.