What would be the best way to apply the bitwise OR operator (or any operator I suppose) to an array of values in javascript?
var array = [1, 5, 18, 4];
// evaluate 1 | 5 | 18 | 4
Use reduce() and pass 0 as the initial value and logical or each value
var array = [1, 5, 18, 4];
var result = array.reduce(function(a, b) {
return a | b;
}, 0);
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With