Array indexOf()
indexOf() is an array search method used to find the first occurrence of a specified element in an array. It takes the element to search for as an argument and returns its index position. If the element is not found, it returns -1. The return type is Number. It is useful when we need to know the position of an element in an array.
let fruits = ["Apple", "Mango", "Orange", "Mango"];
let result = fruits.indexOf("Mango");
console.log(result);//1
let fruits = ["Apple", "Mango", "Orange", "Mango"];
let result =fruits.indexOf("Grapes");
console.log(result);//-1
Array lastIndexOf()
lastIndexOf() is an array search method used to find the last occurrence of a specified element in an array.If the element appears multiple times, only the index of its first occurrence is returned. It takes the element to search for as an argument and returns its index position. If the element is not found, it returns -1. The return type is Number. It is useful when we need to find the last position of an element in an array.
let fruits = ["Apple", "Mango", "Orange", "Mango"];
let result = fruits.lastIndexOf("Mango");
console.log(result);//3
Array includes()
includes() is an array search method used to check whether a specified element exists in an array. It takes the element to search for as an argument and returns true if the element is found; otherwise, it returns false. The return type is Boolean. It is useful when we want to verify the presence of an element in an array.
let fruits = ["Apple", "Mango", "Orange"];
let result = fruits.includes("Mango");
console.log(result);//true
Array find()
find() is an array iteration and search method used to find the first element that satisfies a specified condition.[find() is considered an array iteration method because it iterates through array elements one by one until it finds a matching element.] It executes a callback function for each element in the array and returns the first matching element. If no element satisfies the condition, it returns undefined. The return type is the element itself (such as Number, String, Object, etc.) or undefined. It is useful when we need to retrieve the first matching element from an array.
let nums = [10, 20, 30, 40];
let result = nums.find(num => num > 15);
console.log(result);//20
Array findIndex()
findIndex() is an array iteration and search method used to find the index of the first element that satisfies a specified condition. It executes a callback function for each element in the array and returns the index of the first matching element. If no element satisfies the condition, it returns -1. The return type is Number. It is useful when we need the position of a matching element instead of the element itself.
let nums = [10, 20, 30, 40];
let result = nums.findIndex(num => num > 15);
console.log(result);//1
Array findLast()
findLast() is an array iteration and search method used to find the last element that satisfies a specified condition. It executes a callback function for each element starting from the end of the array and returns the last matching element. If no element satisfies the condition, it returns undefined. The return type is the element itself (Number, String, Object, etc.) or undefined. It is useful when we need the last matching element in an array.
let nums = [10, 20, 30, 40];
let result = nums.findLast(num => num > 15);
console.log(result);//40
Array findLastIndex()
findLastIndex() is an array iteration and search method used to find the index of the last element that satisfies a specified condition. It executes a callback function for each element starting from the end of the array and returns the index of the last matching element. If no element satisfies the condition, it returns -1. The return type is Number. It is useful when we need the position of the last matching element in an array.
let nums = [10, 20, 30, 40];
let result = nums.findLastIndex(num => num > 15);
console.log(result);//3
Top comments (0)