is there a XNOR operator in javascript
Asked Answered
B

5

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)

Bicapsular answered 7/6, 2018 at 13:25 Comment(4)
Isn't it just the equivalent of ==?Islam
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.Hyphenated
Same question in C# - hesitant to close as a dupe (hence why I undid my dupehammer vote) due to it being a different language.Islam
Why are you trying to compare a string and an undefined value with bitwise operators?!Temporal
N
26

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.

Nonchalant answered 7/6, 2018 at 13:32 Comment(3)
@Bicapsular Then use !a === !b if you need to cast your values to proper booleans firstTemporal
I've updated my answer, try this: a == b (note that there are only two equal signs, not three).Nonchalant
that's it @TemporalBicapsular
K
18

The bitwise xnor is:

~(a ^ b)

And the logical one;

a === b
Karlotte answered 7/6, 2018 at 13:43 Comment(0)
R
0

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

Rhizome answered 24/1, 2019 at 23:25 Comment(0)
M
0

Isn't it this simple?

!(A ^ B)

The inverse of XOR.

Maize answered 8/1, 2024 at 16:38 Comment(0)
S
-1

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

Shankle answered 7/6, 2018 at 13:30 Comment(2)
Bitwise not is ~Hyphenated
yeah that what I'm using now, but I wonder if there is an operator that makes it simplerBicapsular

© 2022 - 2025 — McMap. All rights reserved.