7

I ran into a case where I have run both functions in a JavaScript or expression:

function first(){
    console.log("First function");
    return true;
};

function second(){
    console.log("Second function");
    return false;
};

console.log(!!(first()||second()));

In this case it will output:

"First function"

true

In C# there is a logical (|) OR that is different from a conditional or (||) that will make sure both expressions are evaluated:

Func<bool> first = () => { Console.WriteLine("First function"); return true; };
Func<bool> second = () => { Console.WriteLine("Second function"); return false; };
Console.WriteLine(first() | second());

This will output:

In this case it will output:

"First function"

"Second function"

true

I can't seem to find any info on how to implement the same logic in JavaScript without running the expressions beforehand:

function first(){
    console.log("First function");
    return true;
};

function second(){
    console.log("Second function");
    return false;
};

var firstResult = first();
var secondResult = second();

console.log(firstResult||secondResult);

Is there a way I can implement a C# logical OR in JavaScript?

Thanks.

Binil Anto
  • 97
  • 1
  • 11
Philip
  • 160
  • 1
  • 8
  • 4
    "C# logical OR" that's a bitwise or, not a logical or. – Federico klez Culloca Sep 04 '19 at 08:44
  • 1
    Did you try `|` [Bitwise or operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_OR)) – phuzi Sep 04 '19 at 08:44
  • @FedericoklezCulloca: `|` is either a bitwise OR **or** [a logical OR](https://learn.microsoft.com/en-gb/dotnet/csharp/language-reference/operators/boolean-logical-operators#logical-or-operator-) in C# (and many other languages), depending on context. When used as a logical or it's the non-shortcut version. – Joachim Sauer Sep 04 '19 at 08:46
  • @JoachimSauer ok, but it's not usually referred as such. I mean, sure `false | true` and `false || true` are semantically the same, but there's a difference on how they operate. EDIT: per your comment on MrGreek's answer, I think I understand what you mean. – Federico klez Culloca Sep 04 '19 at 08:47
  • @FedericoklezCulloca: yet they are both logical ORs. Granted, the non-shortcut logical OR isn't used a lot (since there's barely a reason to), but it's definitely not a bitwise OR. – Joachim Sauer Sep 04 '19 at 08:49
  • Non-shortcut boolean operators in javascript are `+` for OR and `*` for AND. – georg Sep 04 '19 at 09:04

6 Answers6

10

Just use | (Bitwise OR):

function first(){
    console.log("First function");
    return true;
};

function second(){
    console.log("Second function");
    return false;
};

console.log(!!(first()|second()));

Read more about logical operators (||, !!, etc...) and bitwise operators (|, &, etc...) in JavaScript.

DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
  • 1
    Note that it's not quite the same as in C#: In C# the `|` is used as a non-shortcut logical operator in this case, whereas it's *always* a bitwise OR in JavaScript. You can use it in a similar way, but it doesn't quite behave the same way. – Joachim Sauer Sep 04 '19 at 08:48
  • 1
    I like your answer, but rather than using console.log(!!(first()|second())); I would use console.log(!!(!!first() | !!second())); just in case the functions return something else other than a boolean. – Philip Sep 04 '19 at 09:50
4

You could call all functions by collecting the values and then check the values.

function first(){
    console.log("First function");
    return true;
}

function second(){
    console.log("Second function");
    return false;
}

console.log([first(), second()].some(Boolean));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2
function logicalOr(a, b) {
   return a || b;
}

...

logicalOr(first(), second());

If you call this with functions they evalualed before reaching the or statement.

sebike95
  • 21
  • 2
2

You can use the bitwise or Operator and cast the result to a boolean

let a = () => {console.log("a"); return true}
let b = () => {console.log("b"); return false}
console.log(!!(a()|b()))

This outputs

a

b

true

Community
  • 1
  • 1
1

You are using a bitwise logical operator in c# and conditional OR operator in JavaScript.

use the bitwise logical operator which evaluates both and outputs as expected.

console.log(!!(first()|second()));

Ashish Yadav
  • 1,901
  • 2
  • 17
  • 23
1
function first(){
    console.log("First function");
    return true;
};

function second(){
    console.log("Second function");
    return false;
};

console.log(!!(first() | second()));

// Instead Use this

vaishali
  • 19
  • 2