DEV Community

Cover image for 15 JavaScript Array Functions You Should Master as a Senior Dev

15 JavaScript Array Functions You Should Master as a Senior Dev

Dipak Ahirav on August 14, 2024

JavaScript arrays are a fundamental part of the language, and mastering the array functions it offers is essential for any senior developer. These ...
Collapse
 
tracygjg profile image
Tracy Gilmore • Edited

Nice article Dipak,
For those wanting to know more on this topic, I recommend visiting the authoritative web site: MDN JavaScript Array.
I think it is also worth noting that some developers will highlight the fact that methods such as forEach perform worse than a regular for loop. However, I would argue:

  • If you want performance why are you writing JavaScript (side note, I Love JS).
  • There should be a balance in the code between performance and maintainability.

I would go further to suggest that the declarative style of forEach and map etc. is easier to read and comprehend, but that might just be me.

On the performance issue, one should ask the question "How performant does it need to be?" The usual answer for frontend is "fast enough so the user is not waiting and wondering if the browser has crashed". If the functionality really needs to be in the browser there are other options than using JS in the main thread such as Web Workers or even Web Assembly. On the backend there is no real reason (if performance really is paramount) to be using JS, there are plenty of other options.

Regards.

Collapse
 
petrulunic profile image
PetruLunic

These should know every junior

Collapse
 
alveshelio profile image
Helio Alves

Really, senior developers should know these? I think every developer should know some of them, others shouldn't be used since they mutate the array and we should instead use toSlice, toSort, etc which don't mutate the array.

Collapse
 
alexus85 profile image
Alex Petrik

sigh, just reiterating a well-known topic?

Collapse
 
richantfebriel profile image
richant febriel • Edited

thanks for the explaination, but i still confuse about splice(),

const numbers = [1, 2, 3, 4, 5];
numbers.splice(2, 1, 99);
console.log(numbers); // [1, 2, 99, 4, 5]

in splice parameter, it say '2' for index 2 which means refer to '3', and '99' parameter is refer to what number will be changed to. but '1' in parameter means to what? can someone explain me

btw, sorry for my bad english

Collapse
 
dipakahirav profile image
Dipak Ahirav • Edited

Thank you for your question! @richantfebriel

The second parameter in splice() specifies the number of elements to remove starting at the index given by the first parameter. For example:

const numbers = [1, 2, 3, 4, 5];
numbers.splice(2, 1, 99);
console.log(numbers); // [1, 2, 99, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Here, 1 means remove one element (which is 3). If you change 1 to 2:

const numbers = [1, 2, 3, 4, 5];
numbers.splice(2, 2, 99);
console.log(numbers); // [1, 2, 99, 5]
Enter fullscreen mode Exit fullscreen mode

This removes two elements (3 and 4), and then inserts 99.

I hope this clears up the confusion!

Collapse
 
richantfebriel profile image
richant febriel

thanks for the explanation, easy to understand!

Thread Thread
 
dipakahirav profile image
Dipak Ahirav

your welcome

Collapse
 
allen_gclarke profile image
Allen G. Clarke

Calling it array methods would be okay

Collapse
 
petrfischer profile image
Petr Fischer

Eh... So... In short: every developer should read the javascript documentation for Array and Hash... Surprising! What an article!

Collapse
 
dipakahirav profile image
Dipak Ahirav

Thanks @petrfischer

Collapse
 
androosk profile image
Andrew Tirpok

Nice refresher! Sometimes I forget some of these exist. Also, for...of is useful as well.

Collapse
 
kocreative profile image
Kat

Thanks for the list - certainly some things for me to try here!

Collapse
 
roshan_khan_28 profile image
roshan khan

thanks for the share. this list will help me immensely!

Collapse
 
mohamed_karim_2dddebb42bd profile image
mohamed karim

Thank for sharing

Collapse
 
dmiller72 profile image
David Miller

As a junior dev, I appreciate the curated list for refreshment purposes. A bit easier of a read than MDN docs.

Collapse
 
dipakahirav profile image
Dipak Ahirav

Thanks @dmiller72

Collapse
 
msm_robin_2c623f9163f5552 profile image
MsM Robin

Great, But these are the basic functions. This should be only for junior devs. Those who are senior devs already know all of the functions.

Collapse
 
aavash_parajuli_72 profile image
Aavash Parajuli

Reach++

Collapse
 
softmantk profile image
NIKHIL CM • Edited

Appreciate your effort, but it is not specific to Seniors, every junior devs (should) knows this too. Also, there are several articles about array methods already in dev.to

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

Thanks for sharing

Collapse
 
sajib4414 profile image
Shamsul Arefin

Fantastic

Collapse
 
chandan_e69c011b258e09242 profile image
Chandan

Very informative.
Thanks for Sharing 🤝🏻

Collapse
 
soruj_mahmud_e2b72c204e30 profile image
SORUJ MAHMUD

Useful

Collapse
 
henok_emyaye profile image
Henok Emyaye

That's helpful thanks🙏

Collapse
 
rashamatt profile image
Matt Blais

Don't forget sort()

Collapse
 
wudpecker profile image
Wudpecker

Cool!

Collapse
 
antony_ilin profile image
Antony Ilin

nice article!

Collapse
 
ger_san_f684f719b116f092f profile image
Ger San

Magnifico, conciso y preciso, una guia de consulta rapida.Mil gracias PURA VIDA

Collapse
 
dipakahirav profile image
Dipak Ahirav

Thank you so much! I'm really glad you found it helpful. Pura vida!

Collapse
 
jangelodev profile image
João Angelo

Hi Dipak Ahirav,
Top, very nice and helpful !
Thanks for sharing.

Collapse
 
sleepysheep profile image
shy

对于我这样的初学者来说,这也是有必要掌握的

Some comments have been hidden by the post's author - find out more