How to get the elements of one array which are not present in another array using JavaScript? Last Updated : 23 Jan, 2023 Comments Improve Suggest changes Like Article Like Report The task is to get the elements of one array that are not present in another array. Here we are going to use JavaScript to achieve the goal. Here are a few techniques discussed. Approach Take the arrays in variables.Use the .filter() method on the first array and check if the elements of the first array are not present in the second array, Include those elements in the output. Example 1: This example uses the approach discussed above. html <h1 id="h1" style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Click here </button> <p id="GFG_DOWN"></p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var ar1 = [1, 2, 3, 4]; var ar2 = [2, 4]; el_up.innerHTML = "Click on the button to remove the element of "+ "second from the first array.<br><br> firstArray - " + ar1 + "<br>secondArray - " + ar2; function gfg_Run() { var elmts = ar1.filter( function(i) { return this.indexOf(i) < 0; }, ar2 ); el_down.innerHTML = elmts; } </script> Output: How to get the elements of one array which are not present in another array using JavaScript? Example 2: This example uses the same approach but different methods of JavaScript. html <h1 id="h1" style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Click here </button> <p id="GFG_DOWN"></p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var ar1 = [1, 2, 3, 4]; var ar2 = [2, 4]; el_up.innerHTML = "Click on the button to remove the element of second"+ " from the first array.<br><br> firstArray - " + ar1 + "<br>secondArray - " + ar2; function gfg_Run() { var elmts = ar1.filter(f => !ar2.includes(f)); el_down.innerHTML = elmts; } </script> Output: How to get the elements of one array which are not present in another array using JavaScript? Comment More infoAdvertise with us Next Article How to get the elements of one array which are not present in another array using JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to check whether an array is subset of another array using JavaScript ? The task is to check whether an array is a subset of another array with the help of JavaScript. There are a few approaches, we will discuss below: Approaches:using JavaScript array.every() methodusing JavaScript array.some() methodApproach 1: Using JavaScript array.every() methodThis approach checks 2 min read How to Filter an Array from all Elements of another Array In JavaScript? Filtering one array from all elements of another array is a common task in JavaScript. This involves removing elements from one array that are present in another array. In this article we will explore different approaches to filter an array from all elements of another array in JavaScript.These are 4 min read How to find every element that exists in any of two given arrays once using JavaScript ? In this article, we will learn how to find every element that exists in any of the given two arrays. To find every element that exists in any of two given arrays, you can merge the arrays and remove any duplicate elements. Table of Content Using SetUsing loopUsing filter() and concat()Using reduce a 3 min read How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met 2 min read How to create an array containing non-repeating elements in JavaScript ? In this article, we will learn how to create an array containing non-repeating elements in JavaScript.The following are the two approaches to generate an array containing n number of non-repeating random numbers.Table of ContentUsing do-while loop and includes() MethodUsing a set and checking its si 2 min read How to filter values from an array for which the comparator function does not return true in JavaScript ? The task is to filter the array based on the returned value when passed to the given function. The purpose is to loop through the array and execute one function that will return true or false. Then filter out all the values for which the function (comparator function) does not return true. These are 3 min read JavaScript Program to Find Element that Appears once in Array where Other Elements Appears Twice Given an Array of Integers consisting of n elements where each element appears twice except one element. The task is to find the element which appears only once in the input array in JavaScript. Example: Input : arr = [ 5, 9, 2, 7, 4, 8, 6, 1, 5, 1, 4, 6, 3, 7, 8, 2, 9 ] Output : 3Explanation: The i 3 min read How to Create an Array using Intersection of two Arrays in JavaScript ? In this article, we will try to understand how to create an array using the intersection values of two arrays in JavaScript using some coding examples. For Example: arr1 = [1, 3, 5, 7, 9, 10, 14, 15]arr2 = [1, 2, 3, 7, 10, 11, 13, 14]result_arr = [1, 3, 7, 10, 14]Approach 1: We will see the native a 3 min read How to filter out the non-unique values in an array using JavaScript ? In JavaScript, arrays are the object using the index as the key of values. In this article, let us see how we can filter out all the non-unique values and in return get all the unique and non-repeating elements. These are the following ways by which we can filter out the non-unique values in an arra 4 min read How to check an Array Contains an Object of Attribute with Given Value in JavaScript ? Sometimes, when working with an array of objects in JavaScript, we need to determine whether an object with a specific attribute value exists in the array. This can be useful when filtering or searching for specific objects in the array. Below are the common methods to determine if the array contain 4 min read Like