JavaScript Lessons

JavaScript Array find Method Explained with Examples

The JavaScript array find method returns the first element that passes your test. See how it works with a simple ages array and a clear callback example.

Serhat ALPASLAN1 min read

javascript find usage

The JavaScript find method returns the first element whose value satisfies the condition in the function it runs. In other words, imagine an array containing numbers. To use the find method, we need a function, because it will return the value it gets from the function. Let’s say the numbers in this array are 16, 17, 18, 19, 20. If you define a condition in the function such as a value greater than 18, it will return a single value, namely 19. Now let’s examine this example below;

 

<script>
var sayilar = [16,17,18,19,20];

function sayiKontrol(kontrol){
   return kontrol > 18 ;
}
console.log(sayilar.find(sayiKontrol));
</script>

You can also review it on the official JavaScript website.

Share it if it helped

Write a comment