JavaScript Find and FindIndex Method

 


 Want to understand Properly Plz see the video carefully on YouTube channel name is Adisma

------------------------------------------------------------------------

Definition and Usage

The find() method returns the value of the first element that passes a test.

The find() method executes a function for each array element.

The find() method returns undefined if no elements are found.

The find() method does not execute the function for empty elements.

The find() method does not change the original array.

------------------------------------------------------------

The findIndex() method executes a function for each array element.

The findIndex() method returns the index (position) of the first element that passes a test.

The findIndex() method returns -1 if no match is found.

The findIndex() method does not execute the function for empty array elements.

The findIndex() method does not change the original array.


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1>Find Method</h1>
  <script>
    const ages = [23, 30, 18, 20,70,40,45];

    const result = ages.find(function( curElem){
       return curElem >30
    })

    console.log(result);


    const resultIndex = ages.findIndex(function (curElem) {
        return curElem > 30
      })

      console.log(resultIndex);
 
  </script>
</body>

</html>

Comments

Popular posts from this blog

Javascript Array Concat and Join Method

JavaScript While Loop

JavaScript pop() Method