find() in JavaScript
The find() method is an array method that returns the first element in the array that satisfies the given condition and is undefined if no element matches the given condition. It does not change the array on which it is called and hence helps achieve immutability in the array. This method is available on arrays, strings, and other iterable objects. However, it does not work on objects directly.
Example:
const Numarray = [5, 12, 8, 130, 44];
const data = Numarray.find((x) => x > 10);
console.log(data); // output: 12
const values = [0, false, null, undefined, '', NaN];
const result = values.find((value) => value);
console.log(result); // Output: undefined (no truthy values found, even though there are falsy ones)
Subscribe to my newsletter
Read articles from Dikshya Subedi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by