Coding convention
A coding convention is an agreed set of rules for how code is written: how names are formed, how code is indented, where semicolons go and how files are organised. Conventions do not change what a JavaScript program does, but they make code easier to read, review and fix, especially when several people work on the same project or when you return to your own code months later.
Naming things clearly
JavaScript has a few widely followed naming patterns:
| Kind | Style | Example |
|---|---|---|
| Variables and functions | camelCase | totalPrice, getUserName() |
| Classes and constructors | PascalCase | ShoppingCart, new Date() |
| Fixed configuration constants | UPPER_SNAKE_CASE | MAX_RETRIES, API_URL |
| Booleans | Question-like prefix | isActive, hasAccess, canEdit |
| File names | kebab-case or matching the export | user-profile.js |
Names should say what a value means, not how it is stored. daysUntilExpiry tells a reader far more than d or tempNum. Functions usually start with a verb, such as calculate, fetch or format, because they do something.
Before and after
// Hard to read
var x=function(a,b){if(a>b){return a}else{return b}}
var Data=[1,2,3];var flag=true
// Following common conventions
const getLarger = (first, second) => {
if (first > second) {
return first;
}
return second;
};
const scores = [1, 2, 3];
let isVisible = true;
The second version uses const and let instead of var, descriptive names, spaces around operators, consistent indentation, braces on every if and semicolons at the end of statements.
Layout and formatting habits
- Indent consistently, usually two spaces in JavaScript projects. Never mix tabs and spaces.
- Use
constby default andletonly when a variable is reassigned. Avoidvar, whose function scope causes confusing bugs. - Always use braces for
if,elseand loops, even for one line, so adding a second line later does not silently change the logic. - Use strict equality
===and!==to avoid surprising type conversions. - Pick one quote style, single or double, and use template literals for strings that include variables.
- Keep lines short, roughly 80 to 100 characters, so code fits side by side in reviews.
Comments that earn their place
Good comments explain why, not what. // add 1 to i next to i++ adds noise. // API counts pages from 1, not 0 saves the next developer an hour. If you feel a block of code needs a long explanation of what it does, consider renaming variables or splitting it into a well-named function instead.
The trap of inconsistency
The real danger is not choosing the "wrong" style; it is mixing styles. A file where half the functions use camelCase and half use snake_case, or where indentation jumps between two and four spaces, makes every reader slow down. Another subtle trap is leaving out semicolons without knowing the rules for automatic semicolon insertion. A line starting with ( or [ can join the previous line and break the code. If you skip semicolons, learn those rules; otherwise, use them everywhere.
Let tools enforce the rules
Arguing about spaces in code reviews wastes time. Formatters such as Prettier rewrite code into one consistent layout every time you save. Linters such as ESLint flag problems like unused variables, accidental globals or == comparisons. Many teams adopt a published style guide and let these tools apply it automatically.
Habits to build now
Use meaningful camelCase names, const first, strict equality and braces on every block. Write comments that explain reasons. Above all, stay consistent within a project, and set up a formatter and linter so the conventions are applied without effort.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.