How to Search Arrays with JavaScript

Web Dev
Sponsored links

In this article, I will introduce how to search arrays with JavaScript.

indexOf()

The indexOf method searches from the front whether the same value as the value given to the argument exists in the array, and returns the index number if true and -1 if false.

There are 3 points to note:

  • Returns true only if there is an exact match
  • Uppercase and lowercase letters are distinguished
  • Regular expressions can’t be used

Let's verify the search results in several ways.

Exact match

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
var pattern  'apple';
arr.indexOf(pattern);

//result 0;

Partial match

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
var pattern  'app';
arr.indexOf(pattern);

//result -1;

Case sensitive

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
var pattern  'Apple';
arr.indexOf(pattern);

//result -1;

Regular expression

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
var pattern  '/na/';
arr.indexOf(pattern);

//result -1;
Sponsored links

lastIndexOf()

The lastIndexOf method searches from the back whether the value given as an argument and the same value exists in the array, and returns the index number if true and -1 if false.

There are 3 points to note:

  • Returns true only if there is an exact match
  • Uppercase and lowercase letters are distinguished
  • Regular expressions can’t be used

Let's verify the search results in several ways.

Exact match

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
var pattern  'apple';
arr.lastIndexOf(pattern);

//result 4;

Partial match

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
var pattern  'app';
arr.lastIndexOf(pattern);

//result -1;

Case sensitive

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
var pattern  'Apple';
arr.lastIndexOf(pattern);

//result -1;

Regular expression

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
var pattern  '/na/';
arr.lastIndexOf(pattern);

//result -1;
Sponsored links

every()

The every method tests the callback function specified in the argument and returns true if all the elements in the array meet the condition, and false if there is even one element that does not meet the condition.

Below are samples.

const arr = [1, 2, 3, 4, 5];

arr.every(value => value > 0);
//result true;

arr.every(value => value < 3);
//result false; 
function check(value) {
  return value > 0;
}

const arr = [1, 2, 3, 4, 5];
arr.every(check);

//result true;

arr.every(
  function(value){
    return value > 0;
  }
);

//result true;
Sponsored links

some()

The some method tests the callback function specified in the argument and returns true if even one of the conditions in the array is met, false if not all are met.

Below is a sample.

const arr = [1, 2, 3, 4, 5];
arr.some(value => value === 3);
//result true;

const arr = [1, 2, 3, 4, 5];
arr.some(value => value === 0);
//result false;
Sponsored links

find()

The find method finds if there is an element in the array that satisfies the result of the test function, and returns the first element that appears if it exists, or undefined if it does not exist.

Even if there are multiple elements that satisfy the test function, only the first element that appears is returned.

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];

arr.find(element => element === 'apple');

//result 'apple';
Sponsored links

findIndex()

The findIndex method finds if there is an element in the array that satisfies the result of the test function, and returns the index of the first element that appears if it exists, or undefined if it does not exist.

If there are multiple elements that satisfy the test function, only the index of the element that appears first is returned.

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
arr.findIndex(element => element === 'apple');

//result 0;
Sponsored links

includes()

The includes method returns true if the specified value exists in the array, false otherwise. It does not support IE, so check the requirements before using it.

var arr = ['apple', 'banana', 'grape', 'lemon', 'cherry'];
var pattern = 'apple';
arr.includes(pattern);

//result true;