DEV Community


Check For The Presence of an Element With indexOf()

rthefounding profile image Randy Rivera ・1 min read
  • Since arrays can be changed, or mutated, at any time, there's no guarantee about where a particular piece of data will be on a given array, or if that element even still exists. Luckily, JavaScript provides us with another built-in method, indexOf(), that allows us to quickly and easily check for the presence of an element on an array. indexOf() takes an element as a parameter, and when called, it returns the position, or index, of that element, or -1 if the element does not exist on the array.
  • Let's say for example, I have defined a function, quickCheck, that takes an array and an element as arguments. Modify the function using indexOf() so that it returns true if the passed element exists on the array, and false if it does not. indexOf() can be incredibly useful for quickly checking for the presence of an element on an array.
function quickCheck(arr, elem) {
  // Only change code below this line

  // Only change code above this line

console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
Enter fullscreen mode Exit fullscreen mode
  • Answer:
function quickCheck(arr, elem) {
  if (arr.indexOf(elem) === -1) {
    return false;
  } else {
    return true;

console.log(quickCheck(["onions", "squash", "shallots"], "onions")); will display true because 'onions` is found in the arr
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app