← Back to JavaScript Course | Chapter 12: Reference & Interview | Lesson 3 of 9

JS Style Guide

A style guide is a set of habits for writing neat, consistent code, like handwriting rules for a class. Clean code is easier for everyone to read.

Naming Variables

Use descriptive, intention-revealing variable names (like userCount instead of x) so code reads clearly without needing extra comments to explain what a value represents.

उदाहरण: Naming Variables

javascript
// Bad:
let x = 10;
// Good:
let userCount = 10;
console.log(userCount);

Constants and Variables

Prefer const by default and only use let when a variable's value genuinely needs to change later — this signals intent and helps prevent accidental reassignment bugs, making it obvious at a glance which variables are meant to stay fixed.

उदाहरण: Constants and Variables

javascript
const maxUsers = 100; // never reassigned, use const
let currentUsers = 0;  // will change, use let
currentUsers++;
console.log(maxUsers, currentUsers);

Formatting Code

Consistent formatting (indentation, spacing, semicolon usage) makes a codebase easier to scan and reduces noisy diffs; most teams enforce this automatically with a formatter like Prettier.

उदाहरण: Formatting Code

javascript
function greet(name) {
  return `Hello, ${name}!`; // consistent spacing and semicolons
}
console.log(greet("Sam"));

Functions and Comments

Keep functions small and focused on one task, and use comments to explain *why* code does something non-obvious rather than restating *what* the code already makes clear.

उदाहरण: Functions and Comments

javascript
// Comment explains WHY, not WHAT:
// Using 0.9 because the payment API rounds fees up
const feeRate = 0.9;
function applyFee(amount) {
  return amount * feeRate;
}
console.log(applyFee(100));

Readable Modern JavaScript

Favor modern, readable constructs — arrow functions, destructuring, template literals — where they genuinely clarify intent, but don't force them where a plain function or string is clearer.

उदाहरण: Readable Modern JavaScript

javascript
const nums = [1, 2, 3];
const { length } = nums; // destructuring where it clarifies
console.log(`Total items: ${length}`);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.