Javascript & Typescript Style Guide
A guide for writing consistent and aesthetically pleasing TypeScript and JavaScript code.
Naming
Variable Naming
- Variable names have to be meaningful nouns.
- Variable names must be defined in camelCase format.
//bad
const date;
const yy;
const ms = 60000;
//good
const currentDate;
const year;
const oneMinuteInMiliseconds = 60000;Constant Naming
- Constant names must be defined in CONSTANT_CASE format.
//bad
const maxLength = 10;
//good
const MAX_LENGTH = 10;Class/Function Names
- Class names have to start with UpperCamelCase.
- Class names have to be a noun.
- Functions have to start with verb followed by a noun and be written in
camelCase format.
- get + content explanation of the function
- set + content explanation of the function
- reset + content explanation of the function
- insert + content explanation of the function
- update + content explanation of the function
- delete + content explanation of the function
- create + content explanation of the function
- add + content explanation of the function
- remove + content explanation of the function
- send + content explanation of the function
- init(initiate) + content explanation of the function
- position + content explanation of the function
//Bad
const getColumns = []; //This is not a function
//Good
const columns = [];
//Good
const getColumns = () => [];Boolean returning variables
- The variables that return a Boolean variable must start with one of the
following prefixes:
- is + content explanation of the variable
- has + content explanation of the variable
- does + content explanation of the variable
- check + content explanation of the variable
// Bad
let loading;
// Good
let isLoading;Loops
- Always keep a space after loop and if blocks.
for (let i = 0; i < 10; ++i) {
// ...
}
while (condition) {
// ...
}
if (condition) {
// ...
}Imports
- Use correct syntax for each exported code.
import { type UserType } from './types'; // export type UserType = { }
// or
import type { UserType } from './types'; // export type UserType = { }
import User from './User'; // export default function User() { }
import { User } from './User'; // export const User = () => { }