Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
👑 Awesome one-liners you might find useful while coding.
// By @coderitual
// https://twitter.com/coderitual/status/1112297299307384833
// 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)
// Array destructuring to see matching elements.
let [r, g, b, a] = [255, 0, 0, 255];
// Object destructuring to reduce multiple lines of code to a single line.
let {width, height} = resolution;
// 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
// Same as above
const add42 = n => (console.log(n), n + 42);
// Log variables with names. I love this trick with object ❤️
console.log({ a, b, c, d, e});
// Random hex color
'#'+(~~(Math.random()*0xffffff)).toString(16).padEnd(6,0)
// We love Javascript that's why instead of Math.floor we use
// Note: Use with caution, it won't work for big (>32bit) or negative numbers
~~anyNumber
@mrhammadasif

This comment has been minimized.

Copy link

commented Apr 2, 2019

Some love from my side to do Math.ceil 😄
-~63.16

@mrhammadasif

This comment has been minimized.

Copy link

commented Apr 2, 2019

Your Hex Color generator code randomly generate invalid color codes as well e.g. (#34556, #aabbc). Replace this with this to fix it

'#'+(~~(Math.random()*0xffffff)).toString(16).padEnd(6,0)

@voila

This comment has been minimized.

Copy link

commented Apr 2, 2019

const add42 = n => (console.log(n), number + 42); should be const add42 = n => (console.log(n), n + 42);

@jordrake

This comment has been minimized.

Copy link

commented Apr 2, 2019

const observe = obj => new Proxy(obj, { get: (t, k) => (console.log(k), t[k]) } )

It will return a new object that will act like obj but also logs when properties are accessed, useful for debugging. Logs for gets only, easily extended for sets but gets a bit verbose for one line.

@designerzen

This comment has been minimized.

Copy link

commented Apr 2, 2019

Don't use ~~Number for big (>32bit) or negative numbers - they are not equivalent!

In fact, I wouldn't recommend using it at all as the performance improvement is negligable and the error margin is large, and very challenging to debug.

@kamoroso94

This comment has been minimized.

Copy link

commented Apr 2, 2019

Correction: you should multiply by 16777216, and pad the start with 0's.

// Random hex color
const randColor = () => '#' + Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, 0)
@gaoryrt

This comment has been minimized.

Copy link

commented Apr 3, 2019

// array from 0 to n - 1
const arr = n => [...Array(n).keys()]
arr(5) // [0, 1, 2, 3, 4]
@revelt

This comment has been minimized.

Copy link

commented Apr 3, 2019

imho first one was best

@benjamingr

This comment has been minimized.

Copy link

commented Apr 3, 2019

const sleep = (ms) => (new Promise(resolve => setTimeout(resolve, ms)));

This can be

const sleep = util.promisify(setTimeout);
@martinsebastian

This comment has been minimized.

Copy link

commented Apr 3, 2019

In devtool console I often fancy console.log.bind(console) for pointless logging out args from callbacks.

Like somePromise().then(console.log.bind(console));

Most of the time I use it for REST requests or event observers or simply to test out some quick proof of concept when mutating data.

@LXSMNSYC

This comment has been minimized.

Copy link

commented Apr 4, 2019

You can also inline variable assignments and get the assigned value at the same time:

let flag;

const toggleFlag = () => ( flag = !flag );

console.log(toggleFlag()); // true
console.log(toggleFlag()); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.