Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




find() - FUNCTION


find() Function is used to return the first element of the array that satisfies a given condition.


Let us say, we have a Array that contains five numbers, 5, 8, 2, 81 and 9.

JavaScript find()-Function

And we want to find the element 8 from the Array.


And find() gives us an elegant way to do it.


Let us see in the below example.


Example :



<html>
<body>  
<script>

	function numberCheck(i) {
  		return i == 8
	}

	var x = [5, 8, 2, 81, 9]
	var y = x.find(numberCheck)
	
    document.write(y)
	    
</script>      
</body>
</html>


Output :



  8

So, we have an Array that has 5 numbers.

JavaScript find()-Function

And we want to find the element 8 from the Array.


We have defined a function called numberCheck(), that checks if a number is equal to 8.


function numberCheck(i) {
	return i == 8
}

And we have called the above function from find() function.


var y = x.find(numberCheck)

And find() function iterates 5 times(i.e. The length of the array), calling numberCheck() function.


var y = x.find(numberCheck)

And it the moment it finds 8 the execution is stopped.


Printing 8 as output