Amazon Prime Day August 2020

Javascript find() method with examples

The find() method returns the value of the first element in an array by execute the function. The find() method executes the function once for each element until returns a true value if not returns undefined. 

 Note: 

 1. find() does not execute the function for empty arrays. 
 2. Does not check the remaining values if returns true 
 3. find() does not change the original array. 

 Syntax 

array.find(function(curValue, index, arr),thisVal))
currValue : Required. The value of the current element 
index : Optional. The array index of the current element 
arr : Optional. The array object the current element belongs to
thisVal: Optional.
 

Example

let numArr = [3, 2, 5, 7, 11, 30];

numArr .find((n)=>{
    return  n % 2 === 0;
});
Output : 2, 30

Post a Comment

0 Comments