Format
Naming of files and directories
Use snake_case.
However, for things that constitute a URL, such as HTML, JavaScript, CSS files, image files, and API paths, use kebab-case.
Use single quotes
Both double quotes and single quotes has their own pros and cons. However, achieving consistency within a project is a necessity.
Delimit ordinary string literals with single quotes ('), rather than double quotes (").
Tip: if a string contains a single quote character, consider using a template string to avoid having to escape the quote.
For more information: Google JavaScript Style Guide
console.log("Hello")
console.log('World')
console.log('Hello')
console.log('World')
Don’t append semicolons at the end of lines
For more information: Don’t use Semicolons in TypeScript!
console.log('Hello');
console.log('Hello')
Use template literals
const first_name = 'John'
const last_name = 'Smith'
console.log(first_name + ' ' + last_name)
const first_name = 'John'
const last_name = 'Smith'
console.log(`${first_name} ${last_name}`)
Use white space for readability
for(let i=1;i<=10;i++){
// ...
}
for (let i = 0; i <= 10; i++) {
// ...
}
Use blank lines for readability
function foo(): number {
let sum = 0
for (...) {
const buz = ''
if (...) {
// ...
}
console.log(...)
}
return sum
}
function bar(): void {
// ...
}
function foo(): void {
let sum = 0
for (...) {
const buz = ''
if (...) {
// ...
}
console.log(...)
}
return sum
}
function bar(): void {
// ...
}
Explicit function return type
function foo() {
// ...
return true
}
function foo(): boolean {
// ...
return true
}
Don’t allow unreachable code
function foo(): boolean {
// ...
return true
console.log('Hello')
}
function foo(): boolean {
// ...
return true
}
Variable names, argument names, and function names should be in snake_case
function fooBar(pageCount: number): void {
const maxCount = 5
// ...
}
function foo_bar(page_count: number): void {
const max_count = 5
// ...
}
Explicit member accessibility
class Foo {
bar(): void {
// ...
}
}
class Foo {
public bar(): void {
// ...
}
}