includes VS indexOf
includes() vs indexOf();
includes() and indexOf() used for search item available or not in the array. accept both same parameters. indexOf() returns the position of the first occurrence of a specified value in the string.
it returns -1 if the item never occurs in the array. includes() method determine whether an item available in the array. this method returns a true if the item is available in the array.
includes() vs indexOf() is the indexOf() method check the "==", the includes() check "===" type also.
when we have used.
- if you want to check the position of the item in the array indexOf() is best.
- if you want to check whether the item available in the array, the includes() is best.
includes() use
indexOf Syntax :
string.indexOf(searchvalue, start);
indexOf() example
var indexofArr = ["Orange", "Apple", NaN]; console.log(indexofArr.indexOf(NaN)); Ans : -1
includes Syntax :
string.includes(searchvalue, start);
indexOf() example
var includesArr = ["Orange", "Apple", NaN]; console.log(includesArr.includes(NaN)); Ans : true