How to calculate the XOR of array elements using JavaScript ? Last Updated : 06 Feb, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we are given an array, and the task is to find the XOR of Array elements using JavaScript. There are two approaches to solve this problem which are discussed below: Method 1: It uses a simple method to access the array elements by an index number and use the loop to find the XOR of values of an Array using JavaScript. Example: This example uses a simple method to find the XOR of Array elements using JavaScript. html <!DOCTYPE html> <html> <head> <title> How to Find the XOR of Values of an Array in JavaScript? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <h3> How to Find the XOR of Values of an Array in JavaScript? </h3> <h4> ----Given Array----<br> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] </h4> <button onclick="myGeeks()">Click</button> <p id="gfg"></p> <script> function XOR(input) { if (toString.call(input) !== "[object Array]") return false; var total = Number(input[0]); for(var i=1;i<input.length;i++) { if(isNaN(input[i])){ continue; } total ^= Number(input[i]); } return total; } function myGeeks(item) { document.getElementById("gfg").innerHTML = "----XOR of Array----" + "<br>" + XOR([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); } </script> </body> </html> Output: How to calculate the XOR of array elements using JavaScript ? Using reduce() method: The array reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator. Syntax: array.reduce( function( total, currentValue, currentIndex, arr ), initialValue ) Example: This example uses the array reduce() method to find the XOR of values of an Array using JavaScript. html <!DOCTYPE html> <html> <head> <title> How to Find the XOR of Values of an Array in JavaScript? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <h3> How to Find the XOR of Values of an Array in JavaScript? </h3> <h4> ----Given Array----<br> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] </h4> <button onclick="myGeeks()"> Click Here! </button> <br><br> XOR: <span id="GFG"></span> <script> var arr=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; function ORofArray(XOR, num) { return XOR ^ num; } function myGeeks(item) { document.getElementById("GFG").innerHTML = arr.reduce(ORofArray); } </script> </body> </html> Output: How to calculate the XOR of array elements using JavaScript ? Comment More infoAdvertise with us Next Article How to calculate the XOR of array elements using JavaScript ? S SHUBHAMSINGH10 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Compute the Sum and Average of Elements in an Array in JavaScript? Computing the sum and average of elements in an array involves iterating through the array, accumulating the sum of its elements, and then dividing the sum by the total number of elements to get the average. This can be achieved using various techniques, from basic loops to more advanced functional 3 min read Find the OR and AND of Array elements using JavaScript Given an array and the is task to find the OR and AND operations of the values of an Array using JavaScript. These are the following methods: Using simple for loopUsing reduce() MethodApproach 1: Using simple for loop It uses a simple method to access the array elements by an index number and use th 2 min read Sum and Product of Array elements using JavaScript Given an array and is the task to find the Sum and Product of the values of an Array using JavaScript. Below are the approaches to find the sum and product of array elements using JavaScript:Table of ContentUsing Iterative methodUsing reduce() methodUsing Iterative methodIt uses a simple method to a 3 min read Calculate the length of an associative array using JavaScript In JavaScript, we have normal arrays in which an element is present at a particular index. Whereas Associative arrays are basically Objects in JavaScript where the index is replaced with user-defined keys. Basically, we can say that it stores Key-Value pairs. Syntax: let arr = { key1: 'value'1, key2 4 min read Swapping two array elements in a single line using JavaScript In JavaScript, there exist many ways by which one can swap two array elements. In this article, we will discuss a way in which one can swap two array elements in JavaScript in a single line. The input and output would be as follows. Input: arr = { 10, 20, 40, 30 }Output: arr = { 10, 20, 30, 40 } // 3 min read How to Get the Size of an Array in JavaScript To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntaxconst a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that 2 min read Compute the Bitwise XOR of all Numbers in a Range using JavaScript The complexity of bitwise XOR computation for all the numbers in a specific range is the most frequent difficulty that many people face during the problem-solving process. Problem Description: Given two integers, L and R, find the bitwise XOR of all numbers in the inclusive range from L to R. There 6 min read How to calculate greatest common divisor of two or more numbers/arrays in JavaScript ? In this article, we are given two or more numbers/array of numbers and the task is to find the GCD of the given numbers/array elements in JavaScript. Examples: Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {2, 4, 6, 8} Output : 2 The GCD of three or more numbers equals the product of the prim 2 min read How to compute the average of an array after mapping each element to a value in JavaScript ? Given an array, the task is to compute the average of an array after mapping each element to a value. Input : arr=[2, 3, 5, 6, 7] Output: 4.6 Explanation : (2+3+5+6+7)/5 = 4.6 Input : [5,7,2,7,8,3,9,3] Output: 5.5 Explanation : (5+7+2+7+8+3+9+3)/8 = 5.5 Here are some common approaches: Table of Cont 3 min read JavaScript - Counting Frequencies of Array Elements Here are the various approaches to count the frequencies of array elements in JavaScript.Using an Object (Simple and Efficient)This is the most common approach for counting frequency in an array. Each array element becomes a key in the object, and its value is incremented as the element appears.Java 2 min read Like