XOR operator js

I just started studying IT a month ago, so I’m a complete noob at programming. In our JS course we have a task to create a XOR operator for boolean valules, and I found a bunch of different possible solutions on this website http://www.howtocreate.co.uk/xor.html. The only one of those codes that works for me is ( foo != bar ). None of the other ones work for me, so there has to be something I’m doing wrong. Any help would be greatly appreciated!

Let’s see your code.

Thanks for wanting to help me. I tried all the codes from the linked website.

var foo = 3; 
var bar = 2; 
(foo && !bar) || (!foo && bar); gives me false although it should be true.

var foo = 3; 
var bar = 2; 
(foo||bar ) && !(foo && bar); gives me false although it should be true.

var foo = 3; 
var bar = 2; 
(foo ? !bar : bar); gives me false although it should be true.

var foo = 3;
var bar = 2; 
(foo != bar); the only one that works for me, gives me true!

var foo = 3; 
var bar = 2; 
(!foo != !bar) gives me false although it should be true.

I looked at the link in your original post. Where did you get the idea to use 3 and 2 for your values of foo and bar? Both of your variables have bool values of true. To get your expected results, you need one of them to have a bool value of false.

I must’ve understood it all wrong. I thought it just checked if the value of the two different variables. Just like if I would do foo===bar and get false.

Ah. I see. Try using either foo = 1 & bar = 0, or you could even use foo = true & bar = false. The examples you’ve shared are meant to test bool values compared to each other.

Sorry for the nooby questions, but what exactly is the “point” of the xor checker? Is it just to check the boolean values?

No worries! We were all noobs at one time.

XOR is EXCLUSIVE OR. As you saw in your link, JavaScript doesn’t have an XOR operator. JavaScript only has the || operator.

With || (OR) a comparison will evaluate to true if the expression on either side of the || is true. The comparison starts on the left, so if the first expression is true the operation is finished, and true is returned. If the first expression is false the second expression is evaluated. If it is true, true is returned. If it is also false, false is returned.

With XOR a comparison will only evaluate to true if one of the expressions is true, but not if both expressions are true. Therefore, unlike JavaScript’s ||, both expressions are evaluated regardless of whether or not the first expression is true or false. In order to return true the XOR comparison must evaluate to one true and one false.

The examples you were running are workarounds to simulate the functionality of the XOR operator using a combination of && (AND) and || (OR) along with the ! (NOT) operator.

1 Like

Great, you actually taught me something valuable! I thought all those operators checked if the actual values of the inputs were the same, and not the boolean values of the inputs. I thouth that since 2 and 3 are different numbers, then 2&&3 would be false, but since 2=true boolean and 3=true boolean, then it’s actually true.

1 Like