How to Verify Element Presence in a JavaScript Array

By: fyvo July 24, 2025 JavaScript

Description

This snippet checks if an array contains a specific element. It returns true if found, false otherwise.

Code Snippet

function containsElement(arr, element) {
  return arr.includes(element);
}

// Example usage:
const myArray = [1, 2, 3, 4, 5];
console.log(containsElement(myArray, 3)); // Output: true
console.log(containsElement(myArray, 6)); // Output: false

Discussion (0)