Array in Java script



indexOf() :
----------
>forward search
>return first index number of the element, if matched.
If did not match, it will return -1:

var arr = ["him","shek","wy","but"]

console.log(arr.indexOf("wy"));

lastIndexOf() :
------------
>backward search
>return last index number of the element, if matched.
If did not match, it will return -1:

var arr = ["him","shek","wy","him","but"]

console.log(arr.indexOf("him"));
console.log(arr.lastIndexOf("him"));

includes():
-------------
return boolean values

var arr = ["him","shek","wy","him","but"]

console.log(arr.indexOf("him"));
console.log(arr.includes("him"));

find() - return only one element
----------------------------------

return undefined if not found

const findElem = prices.find((curVal)=>{
return curVal > 400
});

console.log(findElem);

findIndex() :
--------------
return index no of the found element
return -1 if not found

const findElem = prices.findIndex((curVal)=>{
return curVal > 400
});

console.log(findElem);


filter() :
------------
return a new array for all the matched element
empty array if not matched

const findElem = prices.filter((curVal)=>{
return curVal< 400
});

console.log(findElem);


sort() :
-----------
>convert the elements into strings
and then sort in an ascending manner
>shows incorrect value when a number
is sorted

const prices = [90 ,100,40,1005,50,60]
console.log(prices.sort());
















0 Comments