JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Array Search


📘 JavaScript Array Search – Finding Elements in Arrays

JavaScript provides several methods to search for elements in an array, whether by value, condition, or index.

These methods help you:

  • Check if an item exists

  • Get the index of an item

  • Find the first matching element


🔹 Common Search Methods

Method Description Example
indexOf(value) Returns the first index of value, or -1 if not found arr.indexOf("Banana")
lastIndexOf() Returns the last index of a given value arr.lastIndexOf("Apple")
includes(value) Returns true if value exists in the array arr.includes("Mango")
find(callback) Returns the first element that satisfies the condition arr.find(x => x > 20)
findIndex() Returns the index of the first element that satisfies a condition arr.findIndex(x => x === 10)

🔹 Example

let fruits = ["Apple", "Banana", "Mango", "Banana", "Cherry"];

console.log(fruits.indexOf("Banana"));     // 1
console.log(fruits.lastIndexOf("Banana")); // 3
console.log(fruits.includes("Mango"));     // true

let numbers = [5, 10, 15, 20];
let found = numbers.find(n => n > 12);     // 15
let index = numbers.findIndex(n => n > 12);// 2

🔹 Notes

  • indexOf() and includes() check by value only, not by condition.

  • find() and findIndex() work with callback functions.


Practice Questions

Q1. How do you find the first index of "Banana" in an array?

Q2. Which method returns the last occurrence of an item in an array?

Q3. How can you check if "Mango" exists in the array ["Apple", "Mango"]?

Q4. Which method would return true or false if a value exists in an array?

Q5. How do you find the first number greater than 50 in an array?

Q6. What method finds the index of the first element matching a condition?

Q7. How do you get -1 if a searched value is not found?

Q8. What is the output of ["a", "b", "c"].indexOf("b")?

Q9. How do you find the last index of "Apple" in an array with multiple Apples?

Q10. Which method lets you return the actual element (not the index) based on a condition?


JS Array Search Quiz

Q1: Which method returns the first index of a value in an array?

A. search()
B. find()
C. indexOf()
D. match()

Q2: What is returned by indexOf("Orange") if "Orange" is not in the array?

A. 0
B. null
C. undefined
D. -1

Q3: What does includes("Mango") return if "Mango" is present?

A. "Mango"
B. true
C. 1
D. "Yes"

Q4: Which method returns the last position of a given value?

A. indexOf()
B. findLast()
C. lastIndexOf()
D. search()

Q5: Which method returns the first element that satisfies a given test function?

A. find()
B. search()
C. filter()
D. findIndex()

Q6: What is the output of [10, 20, 30].find(x => x > 15)?

A. 20
B. 30
C. 10
D. true

Q7: What does findIndex(x => x === 15) return in [10, 15, 20]?

A. 15
B. 0
C. 1
D. true

Q8: Which method returns true or false based on value existence?

A. check()
B. includes()
C. indexOf()
D. find()

Q9: What is the result of ["a", "b", "c"].includes("d")?

A. true
B. 0
C. -1
D. false

Q10: Which method would you use to find the position of the last "Banana" in ["Banana", "Apple", "Banana"]?

A. indexOf()
B. lastIndexOf()
C. includes()
D. findIndex()

Go Back Top