About this entry
You’re currently reading “Logical XOR in JavaScript,” an entry on Web 3.0, 6 Bladed Razors, 7 Minute Abs
- Published:
- 03.23.07 / 9pm
- Category:
- JavaScript
Bookmark with- del.icio.us
Add to- Digg
Post on
Search on - Technorati
Logical XOR in JavaScript
Of course there wouldn't be an XOR function in JavaScript when I needed one. So I googled around and found an implementation at How To Create. But it only took two arguments. So I rolled my own supporting a variable number of arguments. In my personal stuff, I've put it into a Math library under the YAHOO namespace, and you can decide if you want to do that on your own.
{
var b = false;
for( var j = 0; j <arguments.length; j++ )
{
if( arguments[ j ] && !b ) b = true;
else if( arguments[ j ] && b ) return false;
}
return b;
};
Usage:
xor( true, true ); // false
xor( false, false, true, true, true, false, true, false ); // false
xor( false, false, false, true, false, false, false, false ); // true
Technorati Tags: JavaScript, XOR
subscribe to my blog. You'll save me some bandwidth that way.


No comments
Jump to comment form | comments rss [?] | trackback uri [?]