
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program to find the index of particular element in an array in javascript?
indexOf()
To find the index of any element , indexOf() method is used .The following example gives the index of a particular element(solarCity) from a given array.
Example
<html> <body> <p id="index"></p> <script> var companies = ["Spacex", "Tesla", "SolarCity", "Neuralika"]; var res = companies.indexOf("SolarCity"); document.getElementById("index").innerHTML = res; </script> </body> </html>
Output
2
This method also helps to find the index of an element after a given specified index.The following example searches index of a particular element " solarCity" not from starting but from the given specified index 4.Therefore the index is 6 but not 2.
Example
<html> <body> <p id="index"></p> <script> var companies = ["Spacex", "Tesla", "SolarCity", "Neuralika", "Spacex", "Tesla", "SolarCity", "Neuralika"]; var res = companies.indexOf("SolarCity", 4); document.getElementById("index").innerHTML = res; </script> </body> </html>
Output
6
- Related Questions & Answers
- Write a Golang program to find the frequency of an element in an array
- Write a Golang program to find the frequency of each element in an array
- C# program to find the index of an element in a List
- Get the index of a particular element in an ArrayList in Java
- Write a Golang program to find the element with the minimum value in an array
- Write a program in C++ to find the top K frequent element in an array of integers
- Write a Golang program to search an element in an array
- Get the last index of a particular element in an ArrayList in Java
- Write a program in Go language to find the element with the maximum value in an array
- Write a program to reverse an array in JavaScript?
- Write a Golang program to search an element in a sorted array
- Write a Java program to find the first array element whose value is repeated an integer array?
- Program to find out the index of the most frequent element in a concealed array in Python
- Write a program in C++ to find the most frequent element in a given array of integers
- Inserting element at falsy index in an array - JavaScript
Advertisements