Return to Kroll Design home page

JavaScript Tutorials

 
« Return

exclusive or

^ exclusive or: exactly one part must be true for the expression to be true. ^ is a bitwise exclusive or. JavaScript has no logical operator exclusive or. So that's why these return a 1 or 0, not a true or false:

To convert the result of ^ from 1 or 0 to logical true or false, you can 'not' it twice:

According to web site http://www.howtocreate.co.uk/xor.html, the best solution, that will work with any data type, is the following:

In the examples above, to convert all data types to logical true or false before comparing them, each operand is negated by "!". This turns false to true and vice versa, but that's ok, because the != comparison still works. All that matters to be an exclusive or is that the two operands aren't equal -- one must be true and the other false.



 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM