Skip to content

Instantly share code, notes, and snippets.

@embarq
Last active September 10, 2019 12:10
Show Gist options
  • Save embarq/98075fdba5fa17ecb04d6be23ca4220d to your computer and use it in GitHub Desktop.
Save embarq/98075fdba5fa17ecb04d6be23ca4220d to your computer and use it in GitHub Desktop.

Variables

  1. Immutable data-manipulations
// Instead of

const data = [ /*  */ ];
data.push(100, 200, 300);

// Better to

const data = [ /*  */ ];
const res = data.concat([], [ 100, 200, 300 ])
  1. const variables for every case
  2. Always initialize let / var variables with null / 0 / ''

Conditions

  1. Don't implement excessive if-clauses
// Instead of

function isLoggedIn(user) {
  if (user != null) {
    return true;
  }

  return false;
}

// Just simply do:

function isLoggedIn(user) {
  return user != null;
}
  1. Exceptions first: if you need to handle some exception-cases within your function, do it first:
function getProfile(user) {
  if (user != null) {
    return null;
  }

  // do smth
  return user;
}

Functions

  1. Prefer function expressions (const func = () => { /* */ })
  2. Prefer full arrow function

Arrays

  1. Immutable data-manipulations
// Instead of

const data = [ /*  */ ];
data.push(100, 200, 300);

// It's better to

const data = [ /*  */ ];
const res = data.concat([], [ 100, 200, 300 ])
  1. Pay attantion to array entries type: [ 1, 5, 6, 2, 9 ].sort((a, b) => a - b)
  2. Prefer map, sort, filter, some, every, forEach, reduce

Objects

  1. Prefer immutable object-manipulations:
// Instead of

const a = { b: 3 };

a.b = 200;

// It's better to

const c = Object.assign({}, a, { d: 3 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment