Skip to content
This repository has been archived by the owner on Sep 11, 2019. It is now read-only.

Naming Variables

Keonwoo Kim edited this page May 3, 2019 · 1 revision

The following are basic rules for naming variables:

  1. Use camelCase for a variable and a function, PascalCase for a class, and UPPER_CASE for a constant.
const plusOneMap = new Map([[1, 2], [2, 3]]);

const duplicateString = (x: string) => `${x} and ${x}`;
function triplicateString(x: string) {
  return `${x}, ${x} and ${x}`;
}

class Rectangle {
  static get type() {
    return 'rectangle';
  }
  constructor(height: number, width: number) {
    this.height = height;
    this.width = width;
  }
}

const API_KEY = 'SW4D5m7zTnc5byJsWco4JGERCa4bnkUkvFRfH6Yi';
  1. Use verb for the name of a function, and noun for the name of a variable.
async function getUserDataAsync(name: string) {
  const response = await fetch(`https://api.github.com/users/${name}`);
  const userData = await response.json();
  return data;
}
  1. Long name is usually better than short name.
// Do
const addressListSortedByName = addressList.concat().sort((a, b) => a.localeCompare(b));

// Don't
const addrlsbynm = addrls.concat().sort((a, b) => a.localeCompare(b));
Clone this wiki locally