# 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

```ts

const data = { ... }

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

```

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. 

```ts

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

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

```

---

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.

```ts

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)

```

---

## 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. 

```ts

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)
```

---

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 
- [Follow me on Twitter](https://twitter.com/sun_anshuman)
- [Subscribe to my YouTube channel](https://www.youtube.com/channel/UC9-rIQbbBVxJyTIWPdhK4YA)
- [Follow me on Medium](https://anshuman-bhardwaj.medium.com)

> Until next time

<img width="100%" src="https://media.giphy.com/media/kRkJXYahXjSE0/giphy.gif" />
