Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 using different approaches.
Use the for Loop
We can use the for loop to iterate through the array elements and perform OR or AND operations with all array elements.
Syntax
Users can follow the syntax below to use the for loop to take OR or AND operation of every element.
for (let i = 0; i < array.length; i++) {
result = result || array[i]; // For OR operation
result = result && array[i]; // For AND operation
}
In the above syntax, the 'result' is a resultant value of the OR or AND operation of previous elements.
Example 1: OR Operation
In the example below, we perform the OR operation of all array elements. We initialize the 'result' variable with 0 and use the for loop to iterate through the array elements.
<html>
<body>
<div id="output"></div>
<script>
let output = document.getElementById('output');
function performOR(array) {
let result = 0;
for (let i = 0; i < array.length; i++) {
result = result || array[i];
}
return result;
}
let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
output.innerHTML += "Array: " + JSON.stringify(numbers) + "<br>";
output.innerHTML += "Result of OR Operation: " + performOR(numbers);
</script>
</body>
</html>
Array: [1,2,3,4,5,6,7,8] Result of OR Operation: 1
Example 2: AND Operation
In this example, we perform the AND operation for every array element. We initialize the result variable with the first array element and start iteration from the second element.
<html>
<body>
<div id="output"></div>
<script>
let output = document.getElementById('output');
function performAND(array) {
let result = array[0];
for (let i = 1; i < array.length; i++) {
result = result && array[i];
}
return result;
}
let num = [10, 30, 20, 40, 50];
output.innerHTML += "Array: " + JSON.stringify(num) + "<br>";
output.innerHTML += "Result of AND operation: " + performAND(num);
</script>
</body>
</html>
Array: [10,30,20,40,50] Result of AND operation: 10
Use the While Loop
The while loop is also useful to iterate through the array elements. We can perform AND or OR operations while iterating through the array elements.
Syntax
let index = 0;
while (index < array.length) {
result = result && array[index]; // For AND operation
index++;
}
Example 3: AND Operation with While Loop
In this example, we use the while loop and '&&' operator to find the AND of all array elements.
<html>
<body>
<div id="output"></div>
<script>
let output = document.getElementById('output');
function performAND(array) {
let result = array[0];
let index = 1;
while (index < array.length) {
result = result && array[index];
index++;
}
return result;
}
let array = [2, 3, 1];
output.innerHTML += "Array: " + JSON.stringify(array) + "<br>";
output.innerHTML += "AND operation result: " + performAND(array);
</script>
</body>
</html>
Array: [2,3,1] AND operation result: 1
Use the Array.reduce() Method
The array.reduce() method reduces the array into a single resultant value. This is the most elegant approach to get the AND or OR of all array elements.
Syntax
// For OR operation array.reduce((result, current) => result || current, 0); // For AND operation array.reduce((result, current) => result && current, array[0]);
Example 4: OR Operation with reduce()
In this example, we use the reduce() method to perform OR operation on all array elements.
<html>
<body>
<div id="output"></div>
<script>
let output = document.getElementById('output');
function performOR(array) {
return array.reduce((result, current) => result || current, 0);
}
let num = [0, 0, 5, 0];
output.innerHTML += "Array: " + JSON.stringify(num) + "<br>";
output.innerHTML += "OR operation result: " + performOR(num);
</script>
</body>
</html>
Array: [0,0,5,0] OR operation result: 5
Example 5: AND Operation with reduce()
In this example, we use the reduce() method to perform AND operation on all array elements.
<html>
<body>
<div id="output"></div>
<script>
let output = document.getElementById('output');
function performAND(array) {
return array.reduce((result, current) => result && current, array[0]);
}
let num = [4, 5, 3];
output.innerHTML += "Array: " + JSON.stringify(num) + "<br>";
output.innerHTML += "AND operation result: " + performAND(num);
</script>
</body>
</html>
Array: [4,5,3] AND operation result: 3
Comparison
| Method | Readability | Performance | Use Case |
|---|---|---|---|
| for loop | Good | Fast | Complex logic needed |
| while loop | Good | Fast | Conditional iteration |
| reduce() | Excellent | Moderate | Clean, functional approach |
Conclusion
You can use for loops, while loops, or the reduce() method to perform AND/OR operations on array elements. The reduce() method provides the most readable and functional approach for this operation.
