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.

Quick Alphabet Array


var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

console.log('this is the letter b...', alphabet[1]);
console.log(alphabet);

The above will output:

this is the letter b... b
["a", "b", "c", "d", "e"...']

In ES6 you could use `let` instead of `var`.

Bring any DOM Element to Top


myEl.parentNode.appendChild(myEl);

Bring myEl to the top within its `parentNode`.