.Some() and .Every() in Javascript

Alexander Gabriel
2 min readJun 7, 2021

These are two built-in javascript methods I have recently found that come in handy when checking to see if there is an attribute in that appears once or multiple times. This came particularly handy to me when checking an array of objects. I’ll demonstrate!

Here is an array of objects of some popular space friends with some of their attributes:

const characters = [
{
name: 'Luke Skywalker',
height: 172,
mass: 77,
eye_color: 'blue',
gender: 'male',
},
{
name: 'Darth Vader',
height: 202,
mass: 136,
eye_color: 'yellow',
gender: 'male',
},
{
name: 'Leia Organa',
height: 150,
mass: 49,
eye_color: 'brown',
gender: 'female',
},
{
name: 'Anakin Skywalker',
height: 188,
mass: 84,
eye_color: 'blue',
gender: 'male',
},
];

With the .every() method we are going to check if every character has a mass of under 200.

const shorterThan200 = characters.every( character => character.height < 200)console.log(shorterThan200)

Printing the return value of shorterThan200 will result in either a true or false boolean statement. In this case, it returns false because Darth Vader worked out legs yesterday and is barely making it over 200. Gains!

In this next scenario, we will use the .some() method to check and see wether any of the characters blue eyes.

const anyBlueEyes = characters.some(character => character.eye_color === 'blue)console.log(anyBlueEyes)

Printing the return statement of anyBlueEyes here will give us a true statement. It looks like Anakin passed on his eye color down to his kin.

Hope you learned a bit about some methods that can help you save some lines of code in the future!

--

--