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

JS स्टाइल गाइड

एक style guide साफ, consistent code लिखने की आदतों का एक सेट है, जैसे किसी class के लिए handwriting के नियम। साफ code हर किसी के लिए पढ़ना आसान होता है।

Variables के नाम रखना

descriptive, intention-revealing variable names इस्तेमाल करें (जैसे x के बजाय userCount) ताकि code साफ पढ़ने में आए बिना यह समझाने के लिए अतिरिक्त comments की ज़रूरत के कि कोई value क्या represent करती है।

उदाहरण: Naming Variables

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

Constants और Variables

डिफ़ॉल्ट रूप से const को prefer करें और let सिर्फ तभी इस्तेमाल करें जब किसी variable की value को सच में बाद में बदलना हो — यह इरादा दिखाता है और गलती से reassignment bugs को रोकने में मदद करता है, एक नज़र में यह साफ बनाते हुए कि कौन से variables fixed रहने वाले हैं।

उदाहरण: Constants and Variables

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

Code को Format करना

Consistent formatting (indentation, spacing, semicolon usage) किसी codebase को scan करना आसान बनाती है और noisy diffs कम करती है; ज़्यादातर teams इसे Prettier जैसे किसी formatter से अपने आप लागू करती हैं।

उदाहरण: Formatting Code

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

Functions और Comments

Functions को छोटा और एक काम पर केंद्रित रखें, और comments का इस्तेमाल यह समझाने के लिए करें कि code कोई non-obvious चीज़ *क्यों* करता है, उसे दोबारा बताने के बजाय जो code पहले से साफ करता है कि *क्या* करता है।

उदाहरण: 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

modern, readable constructs को prefer करें — arrow functions, destructuring, template literals — जहाँ वे सच में इरादा साफ करते हों, पर उन्हें वहाँ जबरदस्ती न थोपें जहाँ एक साधारण function या string ज़्यादा साफ हो।

उदाहरण: 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. #}
आम गलतियां
  1. हर जगह var इस्तेमाल करना, जबकि const और let ज़्यादा साफ और safe हैं।
  2. variables का नाम x1 या data2 जैसे unclear short names से रखना।
  3. naming styles मिलाना, जबकि JavaScript आमतौर पर variables के लिए camelCase और classes के लिए PascalCase इस्तेमाल करती है।
🔒

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.