XOR Color Invert


var color = 0xFFFF00;

function toHex(color) { 
  return '#' + ('000000' + color.toString(16)).substr(-6);
}

function onClick() {
  color ^= 0xFFFFFF;
  document.body.style.background = toHex(color);
}

onClick();

document.addEventListener('click', onClick);

Have a look at the slighty different ES6 version:

This is a great trick and a good reason to learn XOR and binary if you don’t already know about those things. The `toHex` function is also kind of snippet worthy in itself.



 
 
 

Leave a Reply

Spam protection by WP Captcha-Free