-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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
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) |
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
indexOf()
and includes()
check by value only, not by condition.
find()
and findIndex()
work with callback functions.
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?
Q1: Which method returns the first index of a value in an array?
Q2: What is returned by indexOf("Orange") if "Orange" is not in the array?
Q3: What does includes("Mango") return if "Mango" is present?
Q4: Which method returns the last position of a given value?
Q5: Which method returns the first element that satisfies a given test function?
Q6: What is the output of [10, 20, 30].find(x => x > 15)?
Q7: What does findIndex(x => x === 15) return in [10, 15, 20]?
Q8: Which method returns true or false based on value existence?
Q9: What is the result of ["a", "b", "c"].includes("d")?
Q10: Which method would you use to find the position of the last "Banana" in ["Banana", "Apple", "Banana"]?