Skip to content

Instantly share code, notes, and snippets.

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.
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
You can’t perform that action at this time.