4 Ways of Finding Elements in a JavaScript Array

4 ways to find an element in an Array, along with some use cases explained with examples.

Anshuman Bhardwaj
JavaScript in Plain English
3 min readFeb 21, 2022

Arrays are the building blocks for data-based web apps. We can store and manipulate data using an Array easily.

Today, we are going to see 4 ways to find an element in an Array, along with some use cases explained with examples.

I know, I know…

1. find()

The find() method returns the first item that returns true for the passed callback condition, otherwise returns undefined if all items return false for the callback condition.

const numbers = [1, 3, 4, 6, 10];numbers.find(element => element > 6); // 10numbers.find(element => element > 10); // undefined

2. findIndex()

The findIndex() method returns the index of the first item that returns true for the passed callback condition, otherwise returns -1 if all items return false for the callback condition.

const numbers = [1, 3, 4, 6, 10];numbers.findIndex(element => element > 6); // 4numbers.findIndex(element => element > 10); // -1

The find methods are useful when you don’t know exactly what you’re looking for, but you know a way to identify it. For instance, you know the user’s email but not the complete object.

3. indexOf()

The indexOf() method returns the index of the first item that matches the passed element, otherwise returns -1.

const numbers = [1, 3, 4, 6, 10];numbers.indexOf(10); // 4numbers.indexOf(16); // -1

The indexOf() uses the same comparison logic as === . Hence, using it on an array of Object will not be a good idea.

4. lastIndexOf

The lastIndexOf() methods similar to indexOf() we saw above, the only difference being it starts the lookup from the tail end of the array.

So, it's a good idea to use lastIndexOf() if you know that the element has a higher chance of being in the latter half.

const numbers = [1, 3, 4, 6, 10];numbers.lastIndexOf(10); // 4numbers.lastIndexOf(16); // -1

The indexOf() methods are useful when you know exactly what you're looking for. For instance, it’s best to use them for primitive data types like numbers or strings.

Summary

To summarise, I would suggest using the following:

  • find(): when you don't know what you're looking for but you know how it should look.
  • findIndex(): to get the index of the element.
  • indexOf(): when you know exactly what you're looking for, and want the index of the element.
  • lastIndexOf(): when you know what you're looking for, you know it's somewhere at the end, and want the index of the element.
  • when the array items are objects, find() and findIndex() are the way to go.

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.

For more such articles, please follow me on Twitter

Until next time!

Resources

MDN docs

Originally published at https://theanshuman.dev on February 21, 2022.

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Written by Anshuman Bhardwaj

Developer Advocate at @commercetools | My monthly newsletter 👉 https://newsletter.theanshuman.dev

No responses yet

Write a response