Skip to main content
deleted 3 characters in body
Source Link
John Kugelman
  • 364.5k
  • 70
  • 555
  • 600

JavaScript traces its ancestry back to C, and C does not have a logical XOR operator, and no language with C-based syntax does either. Mainly because it's not useful. Bitwise XOR is extremely useful, but in all my years of programming I have never needed a logical XOR.

If you have two boolean variables you can mimic XOR with:

if (a != b) 

With two arbitrary variables you could use ! to coerce them to boolean values and then use the same trick:

if (!a != !b) 

That's pretty obscure though and would certainly deserve a comment. Indeed, you could even use the bitwise XOR operator at this point, though this would be far too clever for my taste:

if (!a ^ !b) 

C does not have a logical XOR operator, and no language with C-based syntax does either. Mainly because it's not useful. Bitwise XOR is extremely useful, but in all my years of programming I have never needed a logical XOR.

If you have two boolean variables you can mimic XOR with:

if (a != b) 

With two arbitrary variables you could use ! to coerce them to boolean values and then use the same trick:

if (!a != !b) 

That's pretty obscure though and would certainly deserve a comment. Indeed, you could even use the bitwise XOR operator at this point, though this would be far too clever for my taste:

if (!a ^ !b) 

JavaScript traces its ancestry back to C, and C does not have a logical XOR operator. Mainly because it's not useful. Bitwise XOR is extremely useful, but in all my years of programming I have never needed a logical XOR.

If you have two boolean variables you can mimic XOR with:

if (a != b) 

With two arbitrary variables you could use ! to coerce them to boolean values and then use the same trick:

if (!a != !b) 

That's pretty obscure though and would certainly deserve a comment. Indeed, you could even use the bitwise XOR operator at this point, though this would be far too clever for my taste:

if (!a ^ !b) 
deleted 3 characters in body; added 137 characters in body
Source Link
John Kugelman
  • 364.5k
  • 70
  • 555
  • 600

C does not have a logical XOR operator, and no language with C-based syntax does either. Mainly because it's not useful. Bitwise XOR is extremely useful, but in all my years of programming I have never needed a logical XOR.

If you have two boolean variables you can mimic XOR with:

if (a != b) 

With two arbitrary variables you could use !! to coerce them to boolean values and then use the same trick:

if (!!a != !!b) 

That's pretty obscure though and would certainly deserve a comment. Indeed, you could even use the bitwise XOR operator at this point, though this would be far too clever for my taste:

if (!a ^ !b) 

C does not have a logical XOR operator, and no language with C-based syntax does either. Mainly because it's not useful. Bitwise XOR is extremely useful, but in all my years of programming I have never needed a logical XOR.

If you have two boolean variables you can mimic XOR with:

if (a != b) 

With two arbitrary variables you could use !! to coerce them to boolean values and then use the same trick:

if (!!a != !!b) 

That's pretty obscure though and would certainly deserve a comment.

C does not have a logical XOR operator, and no language with C-based syntax does either. Mainly because it's not useful. Bitwise XOR is extremely useful, but in all my years of programming I have never needed a logical XOR.

If you have two boolean variables you can mimic XOR with:

if (a != b) 

With two arbitrary variables you could use ! to coerce them to boolean values and then use the same trick:

if (!a != !b) 

That's pretty obscure though and would certainly deserve a comment. Indeed, you could even use the bitwise XOR operator at this point, though this would be far too clever for my taste:

if (!a ^ !b) 
Source Link
John Kugelman
  • 364.5k
  • 70
  • 555
  • 600

C does not have a logical XOR operator, and no language with C-based syntax does either. Mainly because it's not useful. Bitwise XOR is extremely useful, but in all my years of programming I have never needed a logical XOR.

If you have two boolean variables you can mimic XOR with:

if (a != b) 

With two arbitrary variables you could use !! to coerce them to boolean values and then use the same trick:

if (!!a != !!b) 

That's pretty obscure though and would certainly deserve a comment.