TL;DR: Use
snake_casefor file names andcamelCasefor HTML attribute values. Prefer modern syntax (arrow functions, template strings, optional chaining, nullish coalescing). Never useany-- useunknowninstead. Favor immutability and destructuring.
All file names must use snake_case.
src/my_class.js ✅ Correct
src/myClass.js ❌ Wrong
Use Prettier for code formatting and ESLint for linting. Every Prettier and ESLint rule is considered part of this guide. Disable rules only in exceptional cases and always leave a comment explaining why.
Use camelCase for the values of id and data-test-subj attributes:
<button id="veryImportantButton" data-test-subj="clickMeButton">Click me</button>The only exception is when dynamically generating values, where hyphens may be used as delimiters:
buttons.map(btn => (
<button
id={`veryImportantButton-${btn.id}`}
data-test-subj={`clickMeButton-${btn.id}`}
>
{btn.label}
</button>
))- Prefer arrow functions over function expressions.
- Prefer template strings over string concatenation.
- Prefer the spread operator (
[...arr]) overarr.slice()for copying arrays. - Use optional chaining (
?.) and nullish coalescing (??) overlodash.getand similar utilities.
Do not reassign variables, modify object properties, or push values to arrays. Instead, create new variables and shallow copies:
// ✅ Good
function addBar(foos, foo) {
const newFoo = { ...foo, name: 'bar' };
return [...foos, newFoo];
}
// ❌ Bad
function addBar(foos, foo) {
foo.name = 'bar';
foos.push(foo);
}Since TypeScript 3.0 introduced the unknown type, there is rarely a valid reason to use any. Replace any with either a generic type parameter or unknown, combined with type narrowing.
Destructuring reduces temporary references and prevents typo-related bugs:
// ✅ Best
function fullName({ first, last }) {
return `${first} ${last}`;
}
// ❌ Bad
function fullName(user) {
const first = user.first;
const last = user.last;
return `${first} ${last}`;
}Avoid accessing array values by index. When direct access is necessary, use array destructuring:
const arr = [1, 2, 3];
// ✅ Good
const [first, second] = arr;
// ❌ Bad
const first = arr[0];
const second = arr[1];