The includes() method determines the presence of a specified element in a Javascript array.
This method returns true when the array contains the element. False if there is nothing in the collection.
var list = ["A", "B", "C", "D", "E"];
console.log(list.includes("A")); // true
console.log(list.includes("A",1)); //returns false, as "A" is not present at index 1.
console.log(list.includes("B",1)); //returns True , as "B" is present at index 1.
console.log(list.indexOf("A") !== -1 ); // true
Array indexOf function
How to determine the exact position and order of an element by using the indexOfJS technique
indexOf() method searches for component in specified array. Returns index of first occurrence. If the collection doesn’t contain element, returns -1.
var JSArr = ["A", "B", "C", "D"];
console.log(JSArr.indexOf("A")) // 0 found at postion 0
console.log(JSArr.indexOf("B")) // 1 found at postion 1
console.log(JSArr.indexOf("F")) // -1 not found
Example 2 shows the function in use.
var JSArr = ["A", "B", "C", "D"];
if (JSArr.indexOf("A") !== -1) {
alert("Value exists");
} else {
alert("Value does not exists");
}
Leave a Reply