Skip to content

Instantly share code, notes, and snippets.

@chvonrohr
Created June 19, 2019 07:06
Show Gist options
  • Save chvonrohr/c5f846e70da348a1431e580d27fcc5e5 to your computer and use it in GitHub Desktop.
Save chvonrohr/c5f846e70da348a1431e580d27fcc5e5 to your computer and use it in GitHub Desktop.
Shorthanders
// #1 - for
let images = [ 'test.jpg', 'dummy.gif' ];
for (let i = 0; i < allImgs.length; i++) {} // long
for (let i of allImgs) { } // short
// #2 - decimal base
1000000 === 1e6
// #3 - default parameters
volume = (l, w = 3, h = 4 ) => (l * w * h);
// #4 - destructuring assignment shorthand
const { store, form, loading, errors, entity:contact } = this.props;
// #5 - spread operator joining (concat) and cloning (slice)
const odds = [1,2,3];
const numsL = [2,4,6].concat(odds); // long
const numsS = [2,4,6, ...odds]; // short
const copyL = odds.slice(); // long
const copyS = [...odds]; // short
// #6 - mandatory parameter
mandatory = () => { throw new Error('Missing parameter!'); }
foo = (bar = mandatory()) => {
return bar;
}
// #7 - bitwise operators
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
Math.floor(4.9) === 4 // long
~~4.9 === 4 // short
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment