javascript how to find in array code example

Example 1: find element in array javascript

const simpleArray = [3, 5, 7, 15];
const objectArray = [{ name: 'John' }, { name: 'Emma' }]

console.log( simpleArray.find(e => e === 7) )
// expected output 7

console.log( simpleArray.find(e => e === 10) )
// expected output undefined

console.log( objectArray.find(e => e.name === 'John') )
// expected output { name: 'John' }

Example 2: object find javascript

function isBigEnough(element) {
  return element >= 15;
}

[12, 5, 8, 130, 44].find(isBigEnough); // 130

Example 3: find number in array js

array.include(numberWhichYouWantToFInd);
// if present it will return true otherwise false