JavaScript Style Guide
Overview
This style guide summarizes the JavaScript coding standards for all coursework. It is enforced by our ESLint configuration. Following these practices will help you pass the autograder and produce readable, professional code. Key ESLint rules are referenced throughout.
Variable Declaration & Assignment
- Prefer
constfor variables that never change.(ESLint:prefer-const) - Use
letwhen reassignment is needed. - Never use
var; it is disallowed.(ESLint:no-var) - Do not declare variables that are unused.(ESLint:
no-unused-vars)
❌ Bad
var total = 0;
var unusedVar = 5; // Unused
total = total + 2;
✅ Good
let total = 0;
// Only use let if variable may change
total += 2;
// Use const if never reassigned
const userName = 'Alice';
Equality & Conditionals
- Always use
===and!==for equality checks.(ESLint:eqeqeq) - Combine if/else blocks to avoid “lonely”
ifstatements.(ESLint:no-lonely-if,unicorn/no-lonely-if) - Prefer ternary operators (
? :) over simple if/else assignments when appropriate.(ESLint:unicorn/prefer-ternary)
❌ Bad
if (score == 5) {
message = "Perfect!";
} else {
// "Lonely" if inside else
if (score == 0) {
message = "Try again.";
}
}
if (flag) {
result = "yes";
} else {
result = "no";
}
✅ Good
if (score === 5) {
message = "Perfect!";
} else if (score === 0) {
message = "Try again.";
}
// Ternary for simple assignments
result = flag ? "yes" : "no";
Function & String Syntax
- Use arrow functions for callbacks where possible.(ESLint:
prefer-arrow-callback) - Always use template literals (
`...`) for string interpolation and avoiding string concatenation.(ESLint:prefer-template)
❌ Bad
arr.map(function(item) {
// String concatenation
return "Value: " + item;
});
✅ Good
// Arrow function + template literal
arr.map(item => `Value: ${item}`);
Advanced & Code Quality
- Destructure properties used repeatedly from objects.(ESLint:
unicorn/consistent-destructuring) - Always include a descriptive error message in
throw new Error().(ESLint:unicorn/error-message) - Avoid disabling ESLint rules without a clear reason.(ESLint:
unicorn/no-abusive-eslint-disable) - Do not use
alert()in application code.(ESLint:no-alert) - Keep return behavior consistent inside functions.(ESLint:
consistent-return)
❌ Bad
// Repeated property access
for (const user of users) {
if (user.isActive) {
console.log(user.name +
" is active");
}
}
// Throwing error with no message
throw new Error();
// Unneeded ESLint disable
// eslint-disable-next-line eqeqeq
let y = 1;
// Using alert in production code
if (x > 10) {
alert("Too big!");
}
✅ Good
// Use destructuring for
// repeated property access
for (const { isActive, name } of users) {
if (isActive) {
console.log(`${name}
is active`);
}
}
// Always provide an error message
throw new Error("Input was empty");
// Only disable ESLint with a clear comment
// eslint-disable-next-line eqeqeq
// -- Must use loose equality
// for this legacy API
let y = getLegacyValue();
// Use a non-blocking notification
// instead of alert in user-facing code
if (x > 10) {
showNotification("Too big!");
}
Linting and Prettier Formatting
This style guide references most of the ESLint rules defined for each of your homework projects. Some homeworks may differ in their ESLint configurations and rules that are active. Any notable rules will be highlighted in the homework specifications.
We highly recommend that you check your code with both ESLint and Prettier before submission. Each homework has configurations provided, and you simply just need to run npm install at the start of each project to get the ESLint and Prettier dependencies. To run ESLint, run the command npm run lint, and to run Prettier, run the command npm run format. These should point out style errors and fix formatting errors, respectively.