Several Ways to Find an Item in an Array in JavaScript

Several Ways to Find an Item in an Array in JavaScript

A very common operation we perform when coding, is trying to check if any item meets the condition? Do you want to check if a specific value is in the array? Or do you want to find the index. Several Ways to Find an Item in an Array in JavaScript.

A very common operation we perform when coding, is trying to determine if an element or item exists in an array.

This can be simple, such as determining if the number 5 exists in an array of integers, or trying to find out if the string "Hello" exists in an array of strings. Slightly more complicated is determining if an object is in an array, which is usually accomplished in a simple manner by having some unique key that identifies each object.

When it comes to searching through an array, not all techniques are the same. And in this particular situation, the order of the array can affect how quickly we can find an item.

Finding An Item In An Unsorted Array

If we need to find an item in an unsorted array there really are not many other options, we have to look at every item. There is no magic way to know without looking at every item. In Javascript, we can accomplish this in many different ways. See below for a few different implementations.

const someData = [2,8,5,4,7,3]

// Loop through all Items and see if we find the item
// Either item is returned or null is returned
const findItem = (find, items) => {
  let found = null;
  for(const item of items){
    if(item === find){
      found = item;
      break;

You can see there are actually many ways, and there are others that could be derived which are not pictured here. These are the most common ways that you can find an item in an array. Overall, all of these result in linear runtime complexity (O(N)), and there is no real improvement on that unless we have a sorted array. We will visit this situation next.

interview coding programming javascript

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

How to Prepare for a Coding Interview in 8 Weeks

As of this writing, the market is tough. We’ve been hit hard with a deadly pandemic that left thousands of people unemployed. It’s layoffs everywhere and the companies are being conservative when it comes to hiring.

Some Non-Code Tips For Coding Interviews

The interview isn’t only about algorithms. Some Non-Code Tips For Coding Interviews. Coding interviews are hard, and algorithms are probably the hardest part. However, it’s not just about knowing the answer, a lot of interviewers are looking at how you tackle the problem. After talking with a lot of seasoned devs, here are some of the things that they look for when interviewing candidates.

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

I Failed My Coding Interview

Coding interviews are a love/hate relationship for most software engineers. There are radical vantage points on what is the right way to interview an incoming software engineer for a specific or general position.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.