Last active
March 17, 2025 14:51
-
-
Save itzjonas/43eb461bf66caf3a0714a8f7d60ee5bb to your computer and use it in GitHub Desktop.
Useful One-Liners (JavaScript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ); | |
// Regex | |
// find type imports without `import type` | |
import\s+\{\s*([^}]+)\s*\}\s*from\s*['"][^'"]*/types[^'"]*['"]; | |
// Find destructured variable | |
const\s+\{[^}]*membership[^}]*\}\s*=\s*([^;]+); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment