Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript Interview Questions: Equality
/*
Write a function that determines whether two integers are equal without using any comparison operators.
Use the bitwise XOR operator.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR
Using XOR, if any of the bits are different, a 1 is returned, otherwise, a 0 is returned.
*/
function isEqual(n1, n2) {
return (n1 ^ n2) === 0;
}
console.log(isEqual(2,2)); //True
console.log(isEqual(1,2)); //False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment