Js Style Guide in JavaScript
? JavaScript Style Guide
A JavaScript Style Guide is a set of best practices and conventions to write clean, readable, and maintainable code. Following a style guide helps teams collaborate better and reduces bugs.
Popular Style Guides
Airbnb JavaScript Style Guide (very popular)
Google JavaScript Style Guide
StandardJS
Key Style Guide Recommendations
1. Indentation & Spacing
Use 2 or 4 spaces per indent (consistently).
Add spaces around operators and after commas.
// Goodfunction add(a, b) { return a + b;}// Badfunction add(a,b){return a+b;}2. Semicolons
Use semicolons at the end of statements to avoid pitfalls.
let x = 10;console.log(x);3. Variable Declarations
Use
constby default.Use
letif the variable will change.Avoid
var.
const name = "Alice";let age = 25;4. Naming Conventions
Use camelCase for variables and functions:
myVariable,getUserName().Use PascalCase for classes:
UserAccount.Use meaningful names.
5. Quotes
Pick either single
'or double"quotes and be consistent.Use backticks
`for template literals.
6. Braces
Use curly braces
{}for all control statements.Opening brace on the same line.
if (condition) { // code} else { // code}7. Functions
Use arrow functions
()=>{}when appropriate.Avoid overly long functions; keep them focused.
8. Comments
Write clear comments for complex code.
Use
//for single-line,/* */for multi-line.
Example Style
"use strict";const greet = (name) => { if (!name) { return "Hello, Guest!"; } return `Hello, ${name}!`;};console.log(greet("Alice"));Tools for Enforcing Style
ESLint: Popular linter for JavaScript.
Prettier: Automatic code formatter.
Want me to help you set up ESLint or show examples from Airbnb style guide?