DEV Community

Cover image for The other array methods in JavaScript
Kenneth Lum
Kenneth Lum

Posted on • Updated on

The other array methods in JavaScript

I remember back in high school, hypothetically, a classmate could memorize all those names, such as Ruby's array methods any and all, and JavaScript's every and some, and he'd score a 95 in the exam when answering programming questions.

While I might have forgotten it is every and some in JavaScript, and got deducted 5 points each, and got 86.

> ["hi", "hello"].every(e => e.includes("h"))
true

> ["hi", "hello"].every(e => e.includes("i"))
false

> ["hi", "hello"].some(e => e.includes("i"))
true

> ["hi", "hello"].some(e => e.includes("X"))
false
Enter fullscreen mode Exit fullscreen mode

So the classmate may tell me, "you know, I know you write better code than I do, but just somehow I got a better score than you." He was just amused.

And it also could be: his 95 is an A, and my 86 is a B. He could get into a tier 1 school, and I could barely make the last of tier 1 schools, or the top of tier 2 schools.

Then back in college, I may swear that I will never let this happen. And I memorize all those words. Does it make me a better programmer? Not much, really.

And I noticed in terms of interviews, if the interviewers saw that you have to find out it is every or some, by asking to use Google, versus somebody who outright typed it out as if he is breathing, then he may get hired, but not you.

It also could mean, he gets a $350k TC (total compensation), while you get $300k.

So here it is, for JavaScript array methods, here they are:

EFFFMRS

The way I remember it: electronic FM Radio Service, EFMRS

so E stands for every, and F stands for all those filter, find, findLast, findIndex, etc, and M is the common map, R is the reduce, and S is the some.

So there it is. Quite easy to remember.

I was originally thinking of eFRS, as I remember FRS was a nice car, as in the photo above.

Or if you just need to remember every, filter, some, as opposed to Ruby's all, select, and any, then you can remember EFS, for Encrypted File System or something.

And with the other traditional ones:
slice, concat, splice, sort

Although, you may not need concat any more, as you can always do [...arr] or [...arr, ...arr2]. Remember that slice always creates a new array, while sort is in place, and it is doing a string compare, so don't call it on an array of numbers and think that it will work. Use sort((a, b) => a - b) instead. Some senior engineers use sort((a, b) => a > b ? 1 : -1) and it does not comply to the comparison function protocol and it can cause problems. splice is a slightly tricky one. The easy part to remember is the index and the number of items to delete, and then comma separated values to be added. The comma separated values are like Math.max(a, b, c), such that if the result you want is from an array of 10,000 items, it may not work and can cause a stack overflow (if you use Math.max(...arr)).

So these could be most of the ones you have to remember, and presumably you already know about push, pop, shift, unshift, reverse, and some more like flat, flatMap.

Is reverse in place or not? One way to remember is, for the traditional methods, only concat and slice give back a new array. All the other ones are in place. (I consider the map etc, to be the newer ones that came out after the traditional ones).

I also just saw that there are new methods called toSorted and toReversed, which give you a new array.

There is also with, so that [1, 3, 5, 7, 9].with(3, 123) will give you back a new array [1, 3, 5, 123, 9], which is suitable for immutability when you use React.

So you can remember these to have some advantages when interviewing.

Is it fair? I may say not. Is it happening? It seems like it is.

Top comments (0)