# Basic Data Structures - Check For The Presence of an Element With indexOf()

**URL:** <https://forum.freecodecamp.org/t/basic-data-structures-check-for-the-presence-of-an-element-with-indexof/601410>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [March 29, 2023, 5:35pm UTC](https://forum.freecodecamp.org/t/basic-data-structures-check-for-the-presence-of-an-element-with-indexof/601410 "2023-03-29T17:35:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishna.kumar245324](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krishna.kumar245324/32/243117_2.png) [@krishna.kumar245324](https://forum.freecodecamp.org/u/krishna.kumar245324)\
**Post date:** [March 29, 2023, 5:35pm UTC](https://forum.freecodecamp.org/t/basic-data-structures-check-for-the-presence-of-an-element-with-indexof/601410/1 "2023-03-29T17:35:06Z")

</div>

* * *

I used if-else to check the index of array elements, and it does work, but it is not passing all the test case.  
Please check my code and tell me where I need to improve.

**Your code so far**

```javascript
function quickCheck(arr, elem) {
  // Only change code below this line
  if(arr.indexOf(elem)){
    return true;
  }else{
    return false;
  }
  // Only change code above this line
}

console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36`

**Challenge:** Basic Data Structures - Check For The Presence of an Element With indexOf()

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/check-for-the-presence-of-an-element-with-indexof)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [March 29, 2023, 5:43pm UTC](https://forum.freecodecamp.org/t/basic-data-structures-check-for-the-presence-of-an-element-with-indexof/601410/2 "2023-03-29T17:43:57Z")

</div>

`indexOf` returns a number. Either the index position of the element or -1 if it doesn’t find the element. Its return value is not suited for boolean coercion. Check the return value is `>= 0`

> **[Array.prototype.indexOf() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)**
>
> The indexOf() method returns the first index at which a
> given element can be found in the array, or -1 if it is not present.

* * *

If you wanted to use the logic you have you would use `includes` which returns `true` or `false`.

> **[Array.prototype.includes() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)**
>
> The includes() method determines whether an array
> includes a certain value among its entries, returning true or
> false as appropriate.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [September 28, 2023, 5:44am UTC](https://forum.freecodecamp.org/t/basic-data-structures-check-for-the-presence-of-an-element-with-indexof/601410/3 "2023-09-28T05:44:34Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
