Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript - How to Get First N Elements from an Array?
Next article icon

JavaScript - Find Index of a Value in Array

Last Updated : 18 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are some effective methods to find the array index with a value in JavaScript.

Using indexOf() - Most Used

indexOf() returns the first index of a specified value in an array, or -1 if the value is not found.

JavaScript
const a = [10, 20, 30, 40, 50];  // Find index of value 30 const index = a.indexOf(30); console.log(index);  // Value not found const notFound = a.indexOf(60); console.log(notFound);  

Output
2 -1 

Using findIndex() for Complex Conditions

findIndex() is used for finding the index of an element based on complex conditions. It takes a callback function that can include any logic for locating an item within the array.

JavaScript
const a = [10, 15, 20, 25, 30];  // Find index of first value greater than 18 const index = a.findIndex(value => value > 18); console.log(index); 

Output
2 

Using for Loop for Custom Search

Using a for loop gives you complete control over how you search for items. You can stop the loop as soon as you find what you’re looking for, making it a flexible option for setting custom rules.

JavaScript
const a = [5, 10, 15, 20, 25]; let index = -1;  // Find index of value 20 for (let i = 0; i < a.length; i++) {     if (a[i] === 20) {         index = i;         break;     } } console.log(index);  

Output
3 

Using lastIndexOf() for Reverse Search

If you need the last occurrence of a value you can use lastIndexOf() method which returns the index of the last match, or -1 if the value is not found. This is useful what an arrays contain duplicate values.

JavaScript
const a = [5, 10, 15, 10, 5];  // Find last index of value 10 const index = a.lastIndexOf(10); console.log(index); 

Output
3 

Importance of Finding Array Indices

Finding array indices is essential for

  • Data Manipulation: Helps identify and modify elements based on their position.
  • Search Optimization: Enables targeted data retrieval for better performance.
  • Conditional Processing: Allows for specific actions on elements that meet criteria.
Find the Array Index with a Value in JavaScript

Next Article
JavaScript - How to Get First N Elements from an Array?

L

laxmigangarajula03
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-array
  • JavaScript-DSA
  • JavaScript-Questions

Similar Reads

  • JavaScript - Find Index of a Value in Array
    Here are some effective methods to find the array index with a value in JavaScript. Using indexOf() - Most Used indexOf() returns the first index of a specified value in an array, or -1 if the value is not found. [GFGTABS] JavaScript const a = [10, 20, 30, 40, 50]; // Find index of value 30 const in
    2 min read
  • JavaScript - How to Get First N Elements from an Array?
    There are different ways to get the first N elements from array in JavaScript. Examples: Input:arr = [1, 2, 3, 4, 5, 6], n = 3 Output: [1, 2, 3] Input:arr = [6, 1, 4, 9, 3, 5, 7], n = 4Output: [6, 1, 4, 9]1. Using slice() MethodThe slice() method is used to extract a part of an array and returns a n
    4 min read
  • How to Copy Array by Value in JavaScript ?
    There are various methods to copy array by value in JavaScript. 1. Using Spread OperatorThe JavaScript spread operator is a concise and easy metho to copy an array by value. The spread operator allows you to expand an array into individual elements, which can then be used to create a new array. Synt
    4 min read
  • What is the most efficient way to concatenate N arrays in JavaScript ?
    In this article, we will see how to concatenate N arrays in JavaScript. The efficient way to concatenate N arrays can depend on the number of arrays and the size of arrays. To concatenate N arrays, we use the following methods: Table of Content Method 1: Using push() MethodMethod 2: Using concat() M
    3 min read
  • How to Merge Two Arrays and Remove Duplicate Items in JavaScript?
    Given two arrays, the task is to merge both arrays and remove duplicate items from merged array in JavaScript. The basic method to merge two arrays without duplicate items is using spread operator and the set constructor. 1. Using Spread Operator and Set() ConstructorThe Spread Operator is used to m
    3 min read
  • How to clone an array in JavaScript ?
    In JavaScript, cloning an array means creating a new array with the same elements as the original array without modifying the original array. Here are some common use cases for cloning an array: Table of Content Using the Array.slice() MethodUsing the spread OperatorUsing the Array.from() MethodUsin
    6 min read
  • JavaScript - Check if JS Array Includes a Value?
    To check if an array includes a value we can use JavaScript Array.includes() method. This method returns a boolean value, if the element exists it returns true else it returns false. 1. Using Array.includes() Method - Mostly Used The JS array.includes() method returns true if the array contains the
    4 min read
  • JavaScript - Create an Object From Two Arrays
    Here are the different methods to create an object from two arrays in JavaScript 1. Using for-each loopThe arr.forEach() method calls the provided function once for each element of the array. [GFGTABS] JavaScript //Driver Code Starts{ const a1 = ['name', 'age', 'city']; const
    3 min read
  • How to remove falsy values from an array in JavaScript ?
    Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja
    6 min read
  • JavaScript - Use Arrays to Swap Variables
    Here are the different methods to swap variables using arrays in JavaScript 1. Using Array DestructuringArray destructuring is the most efficient and modern way to swap variables in JavaScript. This method eliminates the need for a temporary variable. [GFGTABS] JavaScript let x = 5; let y = 10; [x,
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences