Skip to content

Instantly share code, notes, and snippets.

@fogrew
Created May 8, 2016 21:57
Show Gist options
  • Save fogrew/c882a332fb1ed9ae55c18bdbbce17815 to your computer and use it in GitHub Desktop.
Save fogrew/c882a332fb1ed9ae55c18bdbbce17815 to your computer and use it in GitHub Desktop.
JavaScript tricks
// Converting NodeList to Arrays
[].slice.call(document.querySelectorAll(query));
// Shuffling array’s elements
[1,2,3].sort(Math.random() - 0.5); // [2,1,3]
// Caching the array.length in the loop
for(var i = 0, length = array.length; i < length; i++) {
console.log(array[i]);
}
// Merging arrays
var array1 = [1,2,3];
var array2 = [4,5,6];
console.log(array1.push.apply(array1, array2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment