Skip to content

Instantly share code, notes, and snippets.

@itzjonas
Created May 14, 2019 18:41
Show Gist options
  • Save itzjonas/43eb461bf66caf3a0714a8f7d60ee5bb to your computer and use it in GitHub Desktop.
Save itzjonas/43eb461bf66caf3a0714a8f7d60ee5bb to your computer and use it in GitHub Desktop.
Useful One-Liners (JavaScript)
// Remove any duplicates from an array of primitives
const unique = [...new Set(arr)];
// Sleep in async functions. Use: await sleep(2000)
const sleep = (ms) => (new Promise(resolve => setTimeout(resolve, ms)));
// Type this in your code to break chrome debugger in that line.
debugger;
// Just plain english.
[...].every(Number.isFinite);
// Returns all non-falsy values from an array
[...].filter(Boolean);
// Gets an Item from the list and wraps around to the start if n is larger than the list
items[n % items.length];
// Console.log in array function without adding curly braces
const addFortyTwo = number => console.log(number) || number + 42;
// or
const add42 = n => (console.log(n), n + 42);
// Log variables with names.
console,log({ a, b, c, d, e });
// Random hex color
'#' + Math.floor(Math.random() * 0xFFFFFF).toString(16)
// Easy Toggle
let flag; const toggleFlag = () => ( flag = !flag );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment