JavaScript Array Search

Back to JS page

Table of Contents

indexOf() and lastIndexOf()

indexOf() searches an array for an element value and returns its position. lastIndexOf() returns the last index of the element:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Search</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>indexOf() and lastIndexOf()</h4>
<p id="demo"></p>

<script>
const fruits = ["Apple", "Orange", "Apple", "Mango"];

// indexOf() - first occurrence
let index = fruits.indexOf("Apple");

// lastIndexOf() - last occurrence
let lastIndex = fruits.lastIndexOf("Apple");

document.getElementById("demo").innerHTML =
  "Array: " + fruits + "<br><br>" +
  "indexOf('Apple'): " + index + "<br>" +
  "lastIndexOf('Apple'): " + lastIndex + "<br><br>" +
  "indexOf('Banana'): " + fruits.indexOf("Banana") + " (-1 = not found)";
</script>

</body>
</html>

Example 1

Result View Example

includes()

includes() checks if an element is present in the array. It returns true or false:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Search</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>includes()</h4>
<p id="demo"></p>

<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];

document.getElementById("demo").innerHTML =
  "Array: " + fruits + "<br><br>" +
  "includes('Apple'): " + fruits.includes("Apple") + "<br>" +
  "includes('Grape'): " + fruits.includes("Grape") + "<br><br>" +
  "includes() returns true if the element is in the array";
</script>

</body>
</html>

Example 2

Result View Example

find()

find() returns the value of the first element that passes a test function:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Search</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>find()</h4>
<p id="demo"></p>

<script>
const numbers = [4, 9, 16, 25, 29];

// Find the first element > 18
let first = numbers.find(function(value) {
  return value > 18;
});

// Find the first element > 10 and < 20
let first2 = numbers.find(function(value) {
  return value > 10 && value < 20;
});

document.getElementById("demo").innerHTML =
  "Array: " + numbers + "<br><br>" +
  "First > 18: " + first + "<br>" +
  "First > 10 and < 20: " + first2;
</script>

</body>
</html>

Example 3

Result View Example

findIndex()

findIndex() returns the index of the first element that passes a test function:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Search</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>findIndex()</h4>
<p id="demo"></p>

<script>
const numbers = [4, 9, 16, 25, 29];

// Find the index of the first element > 18
let index = numbers.findIndex(function(value) {
  return value > 18;
});

document.getElementById("demo").innerHTML =
  "Array: " + numbers + "<br><br>" +
  "findIndex(v > 18): " + index + " (element is " + numbers[index] + ")";
</script>

</body>
</html>

Example 4

Result View Example

findLast() and findLastIndex()

findLast() returns the value of the LAST element that passes a test. findLastIndex() returns its index:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Search</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>findLast() and findLastIndex()</h4>
<p id="demo"></p>

<script>
const numbers = [4, 9, 16, 25, 29];

// findLast() - last element > 10
let last = numbers.findLast(function(value) {
  return value > 10;
});

// findLastIndex() - index of last element > 10
let lastIndex = numbers.findLastIndex(function(value) {
  return value > 10;
});

document.getElementById("demo").innerHTML =
  "Array: " + numbers + "<br><br>" +
  "findLast(v > 10): " + last + "<br>" +
  "findLastIndex(v > 10): " + lastIndex;
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference