DEV Community

Cover image for 3 Array methods every JavaScript developer should know
Anshuman Bhardwaj
Anshuman Bhardwaj

Posted on • Updated on • Originally published at theanshuman.dev

 

3 Array methods every JavaScript developer should know

Arrays are something all of us come across every day. Today I'll share my picks for 3 of the most uncommonly used Array methods.

isArray

In JavaScript, we have to infer the data type of variables way too often, even more often in nested Objects. One of the ways most JavaScript developers do it (including myself) is to check the length property


const data = { ... }

// true, if arrayKey exists and the arrayKey has a length property
(data?.arrayKey && data.arrayKey.length) 

Enter fullscreen mode Exit fullscreen mode

Although this works, what if I told you there is even a better way to do this?

The Array.isArray(param: any) call checks if the passed value is indeed an array or not and returns a boolean value.


Array.isArray([]); // true
Array.isArray(new Array(22)); // true

Array.isArray(0) // false
Array.isArray({}); // false
Array.isArray(null); // false
Array.isArray(undefined); // false

Enter fullscreen mode Exit fullscreen mode

For the next two, let's consider a situation

You have to rate some students based on a test as follows

  • failed: if all answers were wrong
  • passed: if some answers were correct
  • excellent: if all answers were correct

some

The Array.some() methods run the provided function on each item of the array and return true, if the provided function returns true for any of them, otherwise false.

So in our scenarios, we can apply Array.some() for the second use case.


function isCorrectAnswer(answer) { 
 // return true if the answer was correct, otherwise false 
}

const answers = [{ ... }]

// didStudentPass will be true, if any of the answers were  // correct
const didStudentPass = answers.some(isCorrectAnswer)

Enter fullscreen mode Exit fullscreen mode

every

The Array.every() methods run the provided function on each item of the array and return true, if the provided function returns true for all of them, otherwise false.

Array.every() seems like a perfect fit for the other two scenarios.


function isCorrectAnswer(answer) { 
 // returns true if the answer was correct, otherwise false 
}

function isInCorrectAnswer(answer) { 
 // returns true if the answer was wrong, otherwise false 
}

const answers = [{ ... }]

// didStudentFail will be true, if all of the answers were incorrect    

const didStudentFail = answers.every(isInCorrectAnswer)

// didStudentExcel will be true, if all of the answers were correct    

const didStudentExcel = answers.every(isCorrectAnswer)
Enter fullscreen mode Exit fullscreen mode

That's it for now. I hope you find this article helpful! Should you have any feedback or questions, please feel free to put them in the comments below, I would love to hear and work on them.

For more such content, please follow me on Twitter

Until next time

Top comments (6)

Collapse
 
cski profile image
Patryk Cieszkowski

both Array.prototype.some, and Array.prototype.every could be replaced with Array.prototype.filter. And I argue - should be.

Collapse
 
anshuman_bhardwaj profile image
Anshuman Bhardwaj

yes, you're correct. They are an advancement over filter, while filter returns an array they return a boolean. They have a niche purpose as opposed to filter.

Collapse
 
jacksonkasi profile image
Jackson Kasi

thank bro :)

Collapse
 
anshuman_bhardwaj profile image
Anshuman Bhardwaj

glad you liked it, Jackson

Collapse
 
hossein13m profile image
Hossein Mousavi

Cool article

Collapse
 
anshuman_bhardwaj profile image
Anshuman Bhardwaj

thanks @hossein13m , glad it was helpful.

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but itโ€™s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!