find and findIndex array tutorial in JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>find findIndex Methods JavaScript</title>
<script>
var ages = [10, 23, 19, 20];
var agesTwo = [20, 25, 30, 35];
document.write(ages + "<br><br>");
// find() method will work for one by one value of array and execute for first true condition only and will not execute for remaining values of array
var a = ages.find(checkAdult);
document.write(a + "<br><br>");
function checkAdult(ageVariable) {
return ageVariable >= 18;
}
var c = agesTwo.find(checkAdultThree);
document.write(c + "<br><br>");
function checkAdultThree(ageVariable) {
return ageVariable >= 40;
}
// Main difference between find() and findIndex() method is that find() method will return value and findIndex() method will return Index of any particular true condition value of array
// findIndex() method will work for same as find() method but findIndex() method will return index of particular true condition value of Array
var b = ages.findIndex(checkAdultTwo);
document.write(b + "<br><br>");
function checkAdultTwo(ageVariable) {
return ageVariable >= 18;
}
var d = agesTwo.findIndex(checkAdultFour);
document.write(d + "<br><br>");
function checkAdultFour(ageVariable) {
return ageVariable >= 40;
}
</script>
</head>
<body>
</body>
.png)
.png)
.png)
Comments
Post a Comment