DEV Community

Cover image for JavaScript every() method
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

14 1

JavaScript every() method

Yesterday we had a look at the JavaScript some() method, and today we will focus on its brother every().

The main difference between the two:

  • some(): If at least one matches
  • every(): All must match!

Both of them will give us a boolean value back.

Using the Javascript every() method

Let's start by creating an array of items.

const items = [
  { name: 'T-shirt plain', price: 9 },
  { name: 'T-shirt print', price: 20 },
  { name: 'Jeans', price: 30 },
  { name: 'Cap', price: 5 }
];
Enter fullscreen mode Exit fullscreen mode

Let's say we want to check if all the items have a name.

const haveNames = items.every(item => {
  return item.name;
});

// Returns true
Enter fullscreen mode Exit fullscreen mode

If we now remove the name on our item, it will return false.

Let's take a more accurate example.
We have a list of users, with temperatures, we want to see if everyone is under 37.8, else someone potentially has a risk of Covid-19.

const users = [
  { name: 'Bob', temperature: 36.3 },
  { name: 'Sarah', temperature: 37.9 },
  { name: 'Billy', temperature: 36.9 },
];

const temperature = users.every(user => {
  return user.temperature < 37.8;
});

// Returns false
Enter fullscreen mode Exit fullscreen mode

Whoops! Sarah has a high temperature, so now we get a false back, this means we need to do something.

I hope this shows how one line can beat an array to loop over people.

The syntax for every is as follows:

const new = original.every(function(value));
Enter fullscreen mode Exit fullscreen mode

Inside our function, we can check on certain properties the value has.

And remember:

Stay safe

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (2)

Collapse
 
mutale85 profile image
Mutale85

Never knew about these method. Thanks for sharing.
We can follow each other on twitter.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Hey, glad you like them! ✌️

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay