How to Check if an Element Exists in an Array in JavaScript? Last Updated : 07 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Given an array, the task is to check whether an element present in an array or not in JavaScript. If the element present in array, then it returns true, otherwise returns false.The indexOf() method returns the index of first occurance of element in an array, and -1 of the element not found in array.Syntaxlet isExist = array.indexOf(element) !== -1; JavaScript const array = [1, 2, 3, 4, 5]; const element = 3; const isExist = array.indexOf(element) !== -1; console.log(isExist); Outputtrue Please Read JavaScript Array Tutorial for complete understanding of JavaScript Array.Table of ContentUsing includes() MethodUsing find() MethodUsing some() MethodUsing Array findIndex() MethodUsing for Loop (Brute Force Approach)Using includes() MethodThe includes() method returns a boolean value indicating whether an array includes a certain value among its entries or not. JavaScript const array = [1, 2, 3, 4, 5]; const element = 3; let isExist = array.includes( element ); console.log(isExist); Outputtrue Using find() MethodThe find() method returns the value of the first element in the array that satisfies the provided testing function. JavaScript const array = [ 1, 2, 3, 4, 5 ]; const element = 3; let isExist = array.find(item => item === element) !== undefined; console.log( isExist ); Outputtrue Using some() MethodThe some()method tests whether at least one element in the array passes the test condition by the provided function. JavaScript const array = [ 1, 2, 3, 4, 5 ]; const element = 3; const isExist = array.some(item => item === element); console.log(isExist); Outputtrue Using Array findIndex() MethodThe findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1. JavaScript const array = [ 1, 2, 3, 4, 5 ]; const element = 3; const isExist = array.findIndex(item => item === element) !== -1; console.log(isExist); Outputtrue Using for Loop (Brute Force Approach)This approach iterates over each element of the array and checks if it matches the target element. If a match is found, it returns true; otherwise, it returns false. JavaScript const array = [ 1, 2, 3, 4, 5 ]; const element = 3; let isExist = false; for( let i = 0; i < array.length; i++ ) { if( array[i] === element ) { isExist = true; break; } } console.log(isExist); Outputtrue Comment More infoAdvertise with us Next Article How to Check if an Element Exists in an Array in JavaScript? A amanv09 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-QnA WebTech-FAQs Similar Reads How to Check if a Specific Element Exists in a Set in JavaScript ? To check if a specific element exists in a Set in JavaScript, you can use the has method. The has method returns a boolean indicating whether an element with the specified value exists in the Set Syntax:myset.has(value);Parameters:value: It is the value of the element that has to be checked if it ex 1 min read How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP 2 min read How to check if an element has any children in JavaScript ? The task is to find out whether an element has child elements or not with the help of JavaScript. We're going to discuss a few techniques. ApproachSelect the Parent Element.Use one of the firstChild, childNodes.length, children.length property to find whether an element has a child or not.hasChildNo 2 min read How to Check if element exists in the visible DOM in JavaScript ? This article will show you how to check whether an element exists in the visible DOM or not. For that purpose, there are several methods used but we're going to look at a few of them. Example 1: In this example, the element is searched by document.getElementById('Id') and !! operator is used before 2 min read How to Check if an Item Exists in a Multidimensional Array in JavaScript? To check if an item exists in a multidimensional array in JavaScript, you typically need to use a nested loop, or modern array methods such as Array.prototype.some(), to traverse each sub-array. Here are several ways to achieve this:1. Using Nested LoopsThe traditional way to check for an item in a 3 min read How to check if an array includes an object in JavaScript ? Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array.How to check if an array inclu 3 min read How to Check if a Variable is an Array in JavaScript? To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript 2 min read Check if an array is empty or not in JavaScript These are the following ways to check whether the given array is empty or not:1. The length Property - mostly usedThe length property can be used to get the length of the given array if it returns 0 then the length of the array is 0 means the given array is empty else the array have the elements.Jav 1 min read How to Check Object is an Array in JavaScript? There are two different approaches to check an object is an array or not in JavaScript.1. Using Array.isArray() MethodThe Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false. 1 min read How to Declare an Array in JavaScript? Array in JavaScript are used to store multiple values in a single variable. It can contain any type of data like - numbers, strings, booleans, objects, etc. There are varous ways to declare arrays in JavaScript, but the simplest and common is Array Litral Notations. Using Array Literal NotationThe b 3 min read Like