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.
|
Bitwise or operator – Fringe|
is either a bitwise OR or a logical OR in C# (and many other languages), depending on context. When used as a logical or it's the non-shortcut version. – Incubusfalse | true
andfalse || 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. – Venola+
for OR and*
for AND. – Psychro