JS Lessons

JS Comments

JavaScript Comments ऐसे Notes होते हैं जो Browser नहीं पढ़ता, सिर्फ़ Developer के लिए होते हैं। यानि Comments का काम है — कोड को समझाना, Code Sections को Mark करना, Future Reference के लिए Notes छोड़ना। आसान भाषा में कहें — "Comments वो बातें हैं जो कोड के अंदर लिखी जाती हैं लेकिन चलती नहीं हैं।

Comments क्यों ज़रूरी हैं?

कभी-कभी हमारा Code बहुत बड़ा और Complex हो जाता है। ऐसे में अगर आप Comments नहीं लिखते, तो बाद में खुद को भी समझ नहीं आता कि कौन सी लाइन क्या कर रही है इसलिए Comments Code को Readable और Maintainable बनाते हैं।

Comments के Types

JavaScript में Comments दो तरह के होते हैं

  • Single-Line Comments
  • Multi-Line Comments

Single-Line Comment

Single-Line Comment सिर्फ़ एक लाइन के लिए होता है। इसे // से लिखा जाता है।

Example
<script>
// यह एक Single Line Comment है let name = "Jaswant"; // यह Variable Name रखता है console.log(name); // Output दिखाने के लिए
</script>

Explanation:

  • Browser हर उस Line को Ignore करता है जो // के बाद आती है।

  • एक ही Line में Code और Comment दोनों रख सकते हैं।

Multi-Line Comment

Multi-Line Comment कई लाइनों तक लिखा जा सकता है। इसके लिए /* से शुरू और */ पर खत्म करते हैं। Example

<script>
/* यह Multi-Line Comment है आप इसमें कई बातें लिख सकते हैं Browser इसे Ignore कर देगा */ let x = 10; let y = 20; console.log(x + y); </script>
Explanation:
  • जब कोड बड़ा हो और आपको Block Note लिखना हो, तब ये काम आता है।
  • यह Documentation या Section Explanation के लिए Perfect है।

Extra Tip — Comments को Section Divider की तरह Use करें

आप चाहें तो Comments से Code को Sections में बाँट सकते हैं ताकि आपको समझ आए कौन सा Part किस काम के लिए है

<script>
// 🧮 Calculation Section let a = 5; let b = 10; let result = a + b; // 🧾 Output Section console.log("Result is: " + result); </script>

इससे Code बहुत साफ और Professional दिखता है।

Important: Comments Execute नहीं होते

Comments सिर्फ़ Text होते हैं — Browser इन्हें Execute (चलाता) नहीं है। मतलब इनका Output में कोई Role नहीं। Example

<script>
// alert("This will not run"); alert("This will run"); </script>

सिर्फ़ दूसरी लाइन चलेगी क्योंकि पहली Commented है।

Real-Life Example: HTML + JS में Comments

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Comments Example</h2>

<p id="result"></p>

<script>
// यह Example Output दिखाने के लिए है
let price = 100;
let qty = 3;

/*
नीचे वाली लाइन Total Price दिखाती है
और HTML में Text डालती है
*/
document.getElementById("result").innerHTML = "Total Price = ₹" + (price * qty);
</script>

</body>
</html>

Output: Total Price = ₹300

Comments कब Use करने चाहिए?

  • जब आप किसी Logic को समझाना चाहें
  • जब किसी Complex Formula को Clear करना हो
  • जब Team Work में काम हो रहा हो
  • जब Future में Code बदलना हो सकता है
  • जब Code के किसी हिस्से को Temporarily Disable करना हो

Code Disable करने के लिए Comments

कभी-कभी Testing के लिए किसी Line को Temporarily बंद करना होता है — तब Comment का Use करते हैं

// alert("This alert is disabled for now");
console.log("Program is running fine");

Comment करने से वह Line अब Execute नहीं होगी।

Best Practices (अच्छे कमेंट्स लिखने के नियम)

  • हमेशा Meaningful Comments लिखें
  • बहुत ज़्यादा Unnecessary Comments मत डालें
  • Code और Comment का Alignment सही रखें
  • Section Title Comments (Emoji या Symbols) से Clear बनाएं
  • सिर्फ़ Useful Information डालें, Obvious चीज़ें नहीं

❌ Bad Example:

let x = 10; // यह 10 है 
Good Example:
let x = 10; // Base price in rupees

Summary

Comment Type Symbol Use
Single-Line // Short Notes
Multi-Line /* ... */ Long Explanation
Code Disable // किसी Line को रोकना
JavaScript Comments छोटे लगते हैं, लेकिन यही Code को Readable, Clean और Professional बनाते हैं। “Good Code is not just written, it’s explained well with comments.” Browser इन्हें Ignore करता है Developer इन्हें समझता है Team Work में ये बहुत काम आते हैं