- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the OR and AND of Array elements using JavaScript
In JavaScript, we can use the ‘&&’ operator to perform the AND operation between two elements and the ‘||’ operator to perform OR operation between two elements.
Here, we will learn to perform OR and AND operations between every element of the array.
Use the for Loop
We can use the for loop to iterate through the array elements a nd do an OR operation of the array element with the resultant elements.
Syntax
Users can follow the syntax below to use the for loop to take OR or AND operation of every element.
for () { result = result || array[i]; }
In the above syntax, the ‘result’ is a resultant value of the OR operation of previous i-1 elements.
Example 1
In the example below, we have created the array of numbers. After that, we called the performOR() function by passing the array and its length as arguments.
We have defined the ‘result’ variable and initialized it with zero. After that, we used the for loop to iterate through the array elements. In the for loop, we perform the OR operation of the array element with the ‘result’ variable’s value.
In the output, users can observe the resultant value of the OR operation of all array elements.
<html> <body> <div id = "output"> </div> <script> let output = document.getElementById('output'); function performOR(array, n) { let result = 0; for (let i = 0; i < n; i++) { result = result || array[i]; } return result; } let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; output.innerHTML += "Array: "+ JSON.stringify(numbers); output.innerHTML += "<br>" output.innerHTML += "Result of OR Operation of all elements of above array: " + performOR(numbers, numbers.length); </script> </body> </html>
Example 2
In the example below, we perform the AND operation for every array element. First, we initialized the result variable with the first array element. After that, we start iteration from the second array element using for loop, and perform AND operation of the array element with the value of the ‘result’ variable.
<html> <body> <h3> Using the <i> for loop </i> to perform AND operation for all array elements in JavaScript </h3> <div id = "output"> </div> <script> let output = document.getElementById('output'); function performAND(array, n) { let result = array[0]; for (let i = 1; i < n; i++) { result = result && array[i]; } return result; } let num = [10, 30, 20, 40, 50]; output.innerHTML += "Array: "+ JSON.stringify(num ); output.innerHTML += "<br>" output.innerHTML += "Result of AND operation of all elements of the above array: " + performAND(num, num.length); </script> </body> </html>
Use the While Loop
The while loop is also useful to iterate through the array of numbers like for loop. While iterating through the array elements, we can perform the AND or OR operation of a single array element with the resultant variable.
Syntax
Users can follow the syntax below to use the while loop to find the OR and AND of array elements.
while (n--) { result = result && array[n]; }
In the above syntax, the result variable contains the resultant value of OR and AND operation of array elements.
Example 3
In the example below, we have used the while loop and ‘&&’ operator to find the AND of all array elements. We have initialized the result variable with the last element of the array. After that, we reduce the value of ‘n’ by 1 in every iteration and perfrom the AND operation of the array element with the value of the result variable.
<html> <body> <h3> Using the <i> while loop </i> to perform AND operation for all array elements in JavaScript </h3> <div id = "output"> </div> <script> let output = document.getElementById('output'); function performAND(array, n) { let result = array[n]; while (n--) { result = result && array[n]; } return result; } let array = [2, 3]; output.innerHTML += "The AND operation of all elements of " + array + " is " + performAND(array, array.length - 1); </script> </body> </html>
Use the Array.reduce() Method
The array.reduce() reduces the array into a single resultant value. Here, we can reduce the array such that we can get the AND or OR of all array elements.
Syntax
Users can follow the syntax below to use the array.reduce() method to find the AND or OR of all array elements.
array.reduce((res, current) => res || current, 0);
Example 4
In the example below, performOR() function returns the OR of all array elements. In the function, we have used the reduce() method to reduce the array and take the OR of all elements.
In the output, users can observe the resultant value of OR of all elements.
<html> <body> <h3> Using the <i> array.reduce() </i> method to perform OR operation for all array elements in JavaScript </h3> <div id = "output"> </div> <script> let output = document.getElementById('output'); function performOR(array) { return array.reduce((res, current) => res || current, 0); } let num = [10, 30, 20, 40, 50]; output.innerHTML += "The AND operation of all elements of " + JSON.stringify(num) + " is " + performOR(num); </script> </body> </html>
Example 5
In this example, we take AND of all array elements using the array.reduce() method. We have passed the callback function as the first parameter of the array.reduce() method, and the first array element as a second parameter, representing the initial value of the ‘res’ variable. The ‘res’ variable contains the final value of AND operation of array elements.
<html>
<body>
<h3> Using the <i> array.reduce() </i> method to perform AND operation for all array elements in JavaScript </h3>
<div id = "output"> </div>
<script>
let output = document.getElementById('output');
function performAND(array) {
return array.reduce((res, current) => res && current, array[0]);
}
let num = [4, 5];
output.innerHTML += "The AND operation of all elements of " + JSON.stringify(num) + " is " + performAND(num);
</script>
</body>
</html>
Conclusion
Users learned to find the AND or OR of array elements. In the first approach, we have used the for loop, while loop in the second approach. In the third approach, we used the array.reduce() method, which is more readable code.