javascript indexof array – JavaScript Array indexOf() 5+ Examples

javascript indexof array : The JavaScript array indexOf() method is used to search the position of a particular element in a given array. let’s start Introduction to the JavaScript array indexOf() method.

JavaScript Array indexOf() Syntax is :

array.indexOf(element,index)  

javascript indexof array Examples

Example 1

<script>  
var websites=["gpseoagency","itsolutionstuck","Pakainfo","itsolutionstuck","infinityknow"];  
var result= websites.indexOf("itsolutionstuck");  
document.writeln(result);  
</script>  

Example 2

<script>  
var websites=["gpseoagency","itsolutionstuck","Pakainfo","itsolutionstuck","infinityknow"];  
var result= websites.indexOf("itsolutionstuck",2);  
document.writeln(result);  
</script>  

Example 3

<script>  
var websites=["gpseoagency","itsolutionstuck","Pakainfo","itsolutionstuck","infinityknow"];  
var result= websites.indexOf("google");  
document.writeln(result);  
</script>  

JavaScript Array indexOf and lastIndexOf: Locating an Element in an Array

var playerids = [10, 20, 30, 10, 40, 20];

console.log(playerids.indexOf(10)); // 0
console.log(playerids.indexOf(30)); // 2
console.log(playerids.indexOf(50)); // -1
console.log(playerids.indexOf(20)); // 1

const websites = ["gpseoagency", "itsolutionstuck", "Pakainfo", "infinityknow"];
let index = websites.indexOf("Pakainfo");

const websites = ["gpseoagency", "itsolutionstuck", "Pakainfo", "infinityknow", "Pakainfo"];
let index = websites.indexOf("Pakainfo", 3);

JavaScript array lastIndexOf() method

console.log(playerids.lastIndexOf(10));// 3
console.log(playerids.lastIndexOf(20));// 5
console.log(playerids.lastIndexOf(50));// -1

I hope you get an idea about javascript indexof array.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.