JavaScript - Check if JS Array Includes a Value? Last Updated : 20 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To check if an array includes a value we can use JavaScript Array.includes() method. This method returns a boolean value, if the element exists it returns true else it returns false.1. Using Array.includes() Method - Mostly Used The JS array.includes() method returns true if the array contains the specified value. JavaScript const a = [10, 20, 30, 40, 50]; let n1 = 30, n2 = 45; const r1 = a.includes(n1); const r2 = a.includes(n2); // Display results r1 and r2 console.log(n1, "is Present in Array:", r1); console.log(n2, "is Present in Array:", r2); Output30 is Present in Array: true 45 is Present in Array: false 2. Using Linear Search Algorithm - Naive approachIn the Linear search algorithm, iterate over the array using loops and compare each element of the array with the target value. JavaScript const a = [10, 20, 30, 40, 50]; let n1 = 30, n2 = 45; function check(value) { for (let i = 0; i < a.length; i++) { if (a[i] == value) // return if the value is found return value + " is present"; } return value + " is not present"; } console.log(check(n1)); console.log(check(n2)); Output30 is present in the array. 45 is not present in the array. 3. Using indexOf() functionThe indexOf() function returns the index of the target element in the array if it is present and -1 if not present. JavaScript const a = [10, 20, 30, 40, 50]; let n = 40; // Getting the index of element const index = a.indexOf(n) if ( index >= 0) console.log(n + " is present at", index, "index"); else console.log(n + " is not present."); Output40 is present at 3 index 4. Using Binary SearchThe Binary search algorithm works only on sorted arrays and keeps dividing the array into 2 equal halves and works recursively. JavaScript const a = [10, 20, 30, 40, 50]; let n1 = 30, n2 = 45; // Function to implement binary search function bsearch(arr, l, r, x) { if (r >= l) { // Devide array into 2 parts let mid = l + Math.floor((r - l) / 2); // If found then retrun index if (arr[mid] == x) return mid; // Apply Binary Search on Left part if (arr[mid] > x) return bsearch(arr, l, mid - 1, x); // Apply Binary Search on Right part return bsearch(arr, mid + 1, r, x); } // If not found return -1 return -1; } // To check if 30 is present or not console.log("Is "+ n1 +" present? " + (bsearch(a, 0, a.length, n1) != -1)); // To check if 45 is present or not console.log("Is "+ n2 +" present? " + (bsearch(a, 0, a.length, n2) != -1)); OutputIs 30 present? true Is 45 present? false 5. Using filter() MethodThe filter() method is used with the array to pull out the desired element from the array. We first create the array and use the filter method on an array with a method that checks element is present or not. If the element is present in the array it returns an array with an element else returns an empty array. JavaScript const a = [10, 20, 30, 40, 50]; let n1 = 30, n2 = 45; function check(value) { // Filter out the values from array let filterArr = a.filter(x => x == value); // If there is at least one element in array if (filterArr.length) return value + " is present in the array."; return value + " is not present in the array."; } console.log(check(n1)); console.log(check(n2)); Output30 is present in the array. 45 is not present in the array. 6. Using the find() MethodThe array find() method in JavaScript returns the first element in an array that satisfies the provided testing function. If no elements satisfy the condition, it returns undefined. By checking if the result is not undefined, you can determine if the value exists in the array. JavaScript const a = [10, 20, 30, 40, 50]; let n = 45; // Check the if the return value is undefined const result = a.find(element => element === n) !== undefined; console.log(result); Outputfalse Comment More infoAdvertise with us Next Article JavaScript - Create an Object From Two Arrays arorapranay Follow Improve Article Tags : Searching Technical Scripter JavaScript Web Technologies DSA Arrays Technical Scripter 2022 Arrays javascript-array JavaScript-DSA JavaScript-Questions +7 More Practice Tags : ArraysArraysSearching Similar Reads JavaScript - Find Index of a Value in Array Here are some effective methods to find the array index with a value in JavaScript.Using indexOf() - Most Used indexOf() returns the first index of a specified value in an array, or -1 if the value is not found. JavaScriptconst a = [10, 20, 30, 40, 50]; // Find index of value 30 const index = a.inde 2 min read JavaScript - How to Get First N Elements from an Array? There are different ways to get the first N elements from array in JavaScript.Examples:Input:arr = [1, 2, 3, 4, 5, 6], n = 3 Output: [1, 2, 3] Input:arr = [6, 1, 4, 9, 3, 5, 7], n = 4Output: [6, 1, 4, 9]1. Using slice() MethodThe slice() method is used to extract a part of an array and returns a new 4 min read How to Copy Array by Value in JavaScript ? There are various methods to copy array by value in JavaScript.1. Using Spread OperatorThe JavaScript spread operator is a concise and easy metho to copy an array by value. The spread operator allows you to expand an array into individual elements, which can then be used to create a new array.Syntax 4 min read What is the most efficient way to concatenate N arrays in JavaScript ? In this article, we will see how to concatenate N arrays in JavaScript. The efficient way to concatenate N arrays can depend on the number of arrays and the size of arrays. To concatenate N arrays, we use the following methods:Table of ContentMethod 1: Using push() MethodMethod 2: Using concat() Met 3 min read How to Merge Two Arrays and Remove Duplicate Items in JavaScript? Given two arrays, the task is to merge both arrays and remove duplicate items from merged array in JavaScript. The basic method to merge two arrays without duplicate items is using spread operator and the set constructor.1. Using Spread Operator and Set() ConstructorThe Spread Operator is used to me 3 min read How to clone an array in JavaScript ? In JavaScript, cloning an array means creating a new array with the same elements as the original array without modifying the original array.Here are some common use cases for cloning an array:Table of ContentUsing the Array.slice() MethodUsing the spread OperatorUsing the Array.from() MethodUsing t 6 min read JavaScript - Check if JS Array Includes a Value? To check if an array includes a value we can use JavaScript Array.includes() method. This method returns a boolean value, if the element exists it returns true else it returns false.1. Using Array.includes() Method - Mostly Used The JS array.includes() method returns true if the array contains the s 4 min read JavaScript - Create an Object From Two Arrays Here are the different methods to create an object from two arrays in JavaScript1. Using for-each loopThe arr.forEach() method calls the provided function once for each element of the array. JavaScriptconst a1 = ['name', 'age', 'city']; const a2 = ['Ajay', 25, 'New Delhi']; const res = {}; a1.forEac 3 min read How to remove falsy values from an array in JavaScript ? Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja 6 min read JavaScript - Use Arrays to Swap Variables Here are the different methods to swap variables using arrays in JavaScript1. Using Array DestructuringArray destructuring is the most efficient and modern way to swap variables in JavaScript. This method eliminates the need for a temporary variable.JavaScriptlet x = 5; let y = 10; [x, y] = [y, x]; 3 min read Like