DEV Community

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

Posted on β€’ Originally published at daily-dev-tips.com

10 2

JavaScript find() method

Today we are exploring the find() array method in JavaScript.
I find this method very similar to the some() method.

What it does is it searches the array for a specific hit, but instead of returning a boolean, it will return the first match it finds.

Using the Javascript find() 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 find the first item that is under the price of 10.

const haveNames = items.find(item => {
  return item.price < 10;
});

// { name: 'T-shirt plain', price: 9 }
Enter fullscreen mode Exit fullscreen mode

This can also be written as a one-liner:

const found = items.find(item => item.price < 10);
Enter fullscreen mode Exit fullscreen mode

Some use cases could be the first blog-post with the same category.

To see this in action let's say we are currently viewing this article:

const blog = {
    'name': 'JavaScript find() method',
    'category': 'javascript'
}
Enter fullscreen mode Exit fullscreen mode

And we have an array of blog items like this:

const blogs = [
  {
    'name': 'CSS :focus-within',
      'category': 'css'
  },
  {
    'name': 'JavaScript is awesome',
      'category': 'javascript'
  },
  {
    'name': 'Angular 10 routing',
      'category': 'angular'
  }
];
Enter fullscreen mode Exit fullscreen mode

Now we can use find() to get the first blog item that is related to our one (javascript based).

const related = blogs.find(item => item.category === blog.category);
console.log(related);

// { name: 'JavaScript is awesome', category: 'javascript' }
Enter fullscreen mode Exit fullscreen mode

There you go, an example of how to use the find find() method in JavaScript.

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

Neon image

Resources for building AI applications with Neon Postgres πŸ€–

Core concepts, starter applications, framework integrations, and deployment guides. Use these resources to build applications like RAG chatbots, semantic search engines, or custom AI tools.

Explore AI Tools β†’

Top comments (0)

Sentry image

Make it make sense

Make sense of fixing your code with straight-forward application monitoring.

Start debugging β†’

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay