9

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)

aName
  • 2,751
  • 3
  • 32
  • 61
  • 5
    Isn't it just the equivalent of `==`? – James Thorpe Jun 07 '18 at 13:27
  • 1
    Have you considered using xor and not in chain? Also what is this `var a,b="aa"; a^b`? It looks completely wrong and doesn't give true at all. – ASDFGerte Jun 07 '18 at 13:27
  • [Same question in C#](https://stackoverflow.com/questions/7054124/is-there-xnor-logical-biconditional-operator-in-c) - hesitant to close as a dupe (hence why I undid my dupehammer vote) due to it being a different language. – James Thorpe Jun 07 '18 at 13:29
  • Why are you trying to compare a string and an undefined value with bitwise operators?! – Bergi Jun 07 '18 at 13:50

4 Answers4

25

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.

Brandon Dixon
  • 1,036
  • 9
  • 16
17

The bitwise xnor is:

~(a ^ b)

And the logical one;

a === b
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

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

-1

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

shmnff
  • 647
  • 2
  • 15
  • 31