aName
aName

Reputation: 3043

is there a XNOR operator in javascript

I'm new to js and I wonder if there is a XNOR operator in JS. I tried !(a^b) but it gives weird result. For example:

var a,b="aa"; 
a^b

this code returns true, however, I XNOR returns false.
UPDATE
I need to return true if the two operand are true(different from false values), or the two are false (both equals to : null, undefined,""-empty string- or 0)

Upvotes: 9

Views: 12630

Answers (5)

pravchuk
pravchuk

Reputation: 971

Isn't it this simple?

!(A ^ B)

The inverse of XOR.

Upvotes: 0

Hossein Aghatabar
Hossein Aghatabar

Reputation: 1

try (a^b)==0 i think result of XNOR in javascript is: true^true = 0

Upvotes: 0

Brandon Dixon
Brandon Dixon

Reputation: 1134

XNOR truth table

Above is the truth table for XNOR. If A and B are both FALSE or TRUE, the resulting XNOR is true. Therefore, it seems to me as if simply checking for equality is actually the equivalent of XNOR.

So:

(a === b) = (a XNOR b)

EDIT: to work properly with your conditions: this should work:

a == b

Note that there are two "=", not three, indicating that this is comparing "truthy" values.

Upvotes: 26

Jonas Wilms
Jonas Wilms

Reputation: 138257

The bitwise xnor is:

~(a ^ b)

And the logical one;

a === b

Upvotes: 18

shmnff
shmnff

Reputation: 739

try this (!(A ^ B)) or this (A && B) || (!A && !B)

Upvotes: -1

Related Questions