Tutorialspoint.dev

JavaScript | Array.find() Method

Introduction:
The Array.find() is an inbuilt function in JavaScript which is used to get the value of the first element in the array that satisfies the provided condition.It checks all the elements of the array and whichever the first element satisfies the condition is going to print.
Syntax:

array.find(function(element))

Parameters:
element:
– It is the element of the array on which array.find() function works.
Return value:
It return returns the array element value if any of the elements in the array which satisfy the condition, otherwise it returns undefined.
Browser Support:
Here 2nd column contain int values that are version of the corresponding browser.

Feature Basic support
Chrome 45
Edge Yes
Firefox 25
Internet Explorer No
Opera 32
Safari 8
Android webview Yes
Chrome for Android Yes
Edge mobile Yes
Firefox for Android 4
Opera Android Yes
iOS Safari 8

Examples:
Here the Array.find() method in JavaScript returns the value of the first element in the array that satisfies the provided testing function.
Let’s see JavaScript program:

// input array contain some elements.
var array = [10, 20, 30, 40, 50];
  
// Here find function returns the value of the first element
// in the array that satisfies the provided testing
// function (return element > 10).
var found = array.find(function(element) {
  return element > 20;
});
  
// Printing desired values.
console.log(found);

Output:

> 30

Application:
Whenever we need to get the value of the first element in the array that satisfies the provided testing function that time we use Array.find() method in JavaScript.
Let’s see JavaScript program:

// input array contain some elements.
var array = [2, 7, 8, 9];
  
// Here find function returns the value of 
// the first element in the array that satisfies 
// the provided testing function (return element > 4).
var found = array.find(function(element) {
  return element > 4;
});
  
// Printing desired values.
console.log(found);

Output:

> 7


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter