πŸ“œ

JavaScript

Arrow Function Expression

The arrow function expression (=>) creates anonymous functions who do not have their this, arguments, super, or new.target set. This is especially useful for callbacks where you don’t need all that state.

[1,2,3].forEach( num => {
  // Do something with num.
});

const someFunc = () => { /* do stuff */ };
someFunc();

for Loops

Iterating over elements in an array using for...of:

for (const thing of things) { /* ... */ }

Iterating over elements in an array using forEach:

things.forEach( thing => { /* ... */ } );

Iterating over elements in an array using for:

for (let i=0; i<things.length; i++) { /* ... */ }

Iterate over the properties in an object using for...in:

for (const property in thing) { /* ... */ }

Iterate over the properties in an object using Object.keys() and forEach:

Object.keys( thing ).forEach( property => { /* ... */ });

Modules

CommonJS

Setup exports with the module.exports object:

module.exports = { someFunc1, someFunc2 };

Import a module with the require function:

let { someFunc1 } = require('path/to/module');

ES6

Export things:

// One at a time.
export someFunc1;
export someFunc2;

// Or all together:
export { someFunc1, someFunc2 };

// Or export a single default:
export default mainFunc;

Import things:

import { someFunc1, someFunc2 } from 'path/to/module';

// You can also import everything.
import * as variableName from 'path/to/module';

// Use as to alias an import:
import { someFunc1 as myFunc } from 'path/to/module';

// Import the default export:
import variableName from 'path/to/module';

Multiple Variable Assignment

This creates the variable a and b from the left side and assigns them to the value of the same-named object key on the right.

let { a, b } = { a:32, b:45 };

This does the same with an array:

let [ a, b ] = [ 32, 45 ];

When using object notation you can alias keys to a different variable name:

let { b:bbb } = { a:32, b:45 };

The above creates a bbb variable which has the value 45.

Object Initializer Notation

The official syntax for the object initializer:

let o = {
  key: value,
  variable, // Shorthand for: variable: variable
  property: function (parameters) {},
  property (parameters) {}, // Shorthand for the above.
  get property() {},
  set property(value) {},
  ["a"+"b"]: 'A computed key of ab.'
};

These two lines are the same:

let someObj = { a, b };
let someObj = { a:a, b:b };

Prototype and Constructor Inheritance

Given:

function Animal () {}
function Human () {}
Human.prototype.__proto__ = Animal.prototype;
var person = new Human();

Then:

person.__proto__ === Human.prototype;
person.constructor === Human;

Human.prototype.constructor === Human;
Human.prototype.__proto__ === Animal.prototype;

Animal.prototype.constructor === Animal;
Animal.prototype.__proto__ === Object.prototype;

Spread Syntax

The spread syntax takes an array or object and expands it into its elements/properties.

These two lines are identical:

let foo = [ 1, 2, 3 ];
let foo = [ ...[1, 2, 3] ];

A shallow clone of an object:

const clone = { ...someObject };

Building arguments with defaults:

const args = { defKey: 'defVal', ...customArgs }; // Right side wins.

Rest Parameters

Rest parameters are used to gather multiple arguments to a function into a single array.

function bar (firstNum, ...nums) { /* nums is [2,3] */ }
bar( 1, 2, 3 );

Cascading Inherited Arguments

Spread syntax and rest parameters, together, provide the ability to cascade arguments:

function doThingA (firstArg, ...args) {
  firstArg = this.modifyFirstArg( firstArg );
  return this.doThing( firstArg, ...args );
}