Logical OR in JavaScript [duplicate]
Asked Answered
C

6

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.

Cursor answered 4/9, 2019 at 8:40 Comment(6)
"C# logical OR" that's a bitwise or, not a logical or.Venola
Did you try | Bitwise or operatorFringe
@FedericoklezCulloca: | 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.Incubus
@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.Venola
@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.Incubus
Non-shortcut boolean operators in javascript are + for OR and * for AND.Psychro
V
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.

Vacant answered 4/9, 2019 at 8:44 Comment(2)
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.Incubus
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.Cursor
F
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));
Faina answered 4/9, 2019 at 8:44 Comment(0)
G
2
function logicalOr(a, b) {
   return a || b;
}

...

logicalOr(first(), second());

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

Grapeshot answered 4/9, 2019 at 8:45 Comment(1)
This is actually the best answer!Psychro
B
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

Bagatelle answered 4/9, 2019 at 8:54 Comment(0)
B
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()));

Be answered 4/9, 2019 at 8:48 Comment(0)
P
1
function first(){
    console.log("First function");
    return true;
};

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

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

// Instead Use this

Putto answered 4/9, 2019 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.