# Checking to see if a given number exists in an array

**URL:** <https://forum.codewithmosh.com/t/checking-to-see-if-a-given-number-exists-in-an-array/13625>\
**Category:** JavaScript\
**Created:** [July 12, 2022, 2:18pm UTC](https://forum.codewithmosh.com/t/checking-to-see-if-a-given-number-exists-in-an-array/13625 "2022-07-12T14:18:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Micheal](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@Micheal](https://forum.codewithmosh.com/u/Micheal)\
**Post date:** [July 12, 2022, 2:18pm UTC](https://forum.codewithmosh.com/t/checking-to-see-if-a-given-number-exists-in-an-array/13625/1 "2022-07-12T14:18:40Z")

</div>

if i run this code, it keeps showing me undefined . Please what should i do.

const numbers = [1, 2, 3, 4];

function include(array, searchElement) {  
array.forEach(i =\> {  
return i === searchElement;  
});  
}

console.log(include(numbers, 2));

---

<div class="post-metadata">

**Author:** ![Augustine\_Awuori](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/augustine_awuori/32/873_2.png) [@Augustine\_Awuori](https://forum.codewithmosh.com/u/Augustine_Awuori)\
**Post date:** [July 12, 2022, 2:36pm UTC](https://forum.codewithmosh.com/t/checking-to-see-if-a-given-number-exists-in-an-array/13625/2 "2022-07-12T14:36:04Z")

</div>

forEach is a consumer. It does not produce(return)

---

<div class="post-metadata">

**Author:** ![Potato86](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@Potato86](https://forum.codewithmosh.com/u/Potato86)\
**Post date:** [July 12, 2022, 9:11pm UTC](https://forum.codewithmosh.com/t/checking-to-see-if-a-given-number-exists-in-an-array/13625/3 "2022-07-12T21:11:13Z")

</div>

function include(array, searchElement) {  
array.forEach((element) =\> {  
if (element === searchElement) {  
console.log(`${element} is in array`);  
} else {  
console.log(`${element} is not in array`);  
}  
});  
}

console.log(include(numbers, 2));

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [July 16, 2022, 4:19am UTC](https://forum.codewithmosh.com/t/checking-to-see-if-a-given-number-exists-in-an-array/13625/4 "2022-07-16T04:19:38Z")

</div>

If the purpose of the function is to return a boolean indicating that the element was included in the array, then @Potato86’s answer will not quite work.

Personally, I would write the function using the `find()` method:

```javascript
function include(array, searchElement) {
  return array.find(element => element === searchElement) !== undefined;
}

```

If you must loop through the elements manually, you could write it like this:

```javascript
function include(array, searchElement) {
  for (const element of array) {
    if (element === searchElement) {
      return true;
    }
  }
  return false;
}

```

Both of these would allow you to call `console.log(include(numbers, 2));` and have it print out `true`.
