How do I check if an array contains an element in JavaScript?

To check if an array contains a specified value in JavaScript, you can use the array.includes(), array.indexOf(), array.find(), and array.filter() methods. The array.includes() method allows you to determine if the array contains the element you are looking for and returns the boolean "true" or "false". The array.indexOf() method allows you to determine if the JavaScript array contains the specified element and returns the index of the first match, or "-1". By default, the indexOf() method starts searching at the beginning of the array; to search from the end of the array, you can use the lastIndexOf() method. The array.find() method returns the first element in the array that matches the condition according to the callback function, or "undefined". The array.filter() method allows you to create a new array containing the elements from the original array that match the condition specified in the passed function. In this JavaScript Check If Array Contains Element example, we use the array.includes() method. Below you can see more examples of checking if an array contains an element with a detailed description of each method. Click Execute to run the JavaScript Array Contains Example online and see the result.
Loading...