← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 6 of 12

JS टिप्पणियाँ

काम पर किसी shared project पर sticky notes छोड़ने की कल्पना करें, यह समझाते हुए कि कोई decision क्यों लिया गया, किसी colleague को किसी tricky हिस्से के बारे में चेताते हुए, या फिलहाल इस्तेमाल न हो रहे किसी step को अस्थायी रूप से काटते हुए। JavaScript में comments बिल्कुल यही हैं, आपके code में लिखे गए notes जिन्हें browser script चलाते समय पूरी तरह नज़रअंदाज़ कर देता है, पर जो बाद में code पढ़ने वाले किसी भी इंसान के लिए, आने वाले आप सहित, बेहद कीमती होते हैं। अच्छे comments किसी decision के पीछे 'क्यों' समझाते हैं, सिर्फ 'क्या' नहीं, और cookiescursor.com जैसे projects पर काम करने वाली international teams अलग-अलग time zones में एक-दूसरे के साथ sync रहने के लिए साफ comments पर निर्भर रहती हैं।
Syntax
javascript
// single-line comment

/* multi-line
   comment */

Single Line Comments

Single line comment दो forward slashes // से शुरू होता है, और उस बिंदु से line के अंत तक सब कुछ JavaScript engine द्वारा अनदेखा कर दिया जाता है।

Note: Single line comments का उपयोग उस code की line के ठीक ऊपर या बगल में छोटे, त्वरित नोट्स के लिए कीजिए जिसे वे समझाते हैं।
Warning: A // inside a string, like a URL, is just text and does not start a comment, comments only apply to actual code.

उदाहरण: Single Line Comments

javascript
// This line explains the code below
let age = 25;
console.log(age);

Multi Line Comments

Multi line comment /* से शुरू होकर */ पर खत्म होता है, जिससे comment कई lines में फैल सकता है, जो लंबी व्याख्याओं या code के किसी block को अस्थायी रूप से अक्षम करने के लिए उपयोगी है।

Note: लंबी व्याख्याओं के लिए multi line comments का उपयोग कीजिए, जैसे किसी पूरे function या file का उद्देश्य बताना।
Warning: Multi line comments cannot be nested, starting a new /* inside an existing comment does not create an inner comment and can cause confusing errors.

उदाहरण: Multi Line Comments

javascript
/*
  This is a longer explanation
  spanning multiple lines.
*/
console.log("Done");

Code को Comment Out करना

Comments का आमतौर पर उपयोग किसी line या code के block को बिना मिटाए अस्थायी रूप से अक्षम करने के लिए होता है, जो debugging या वैकल्पिक तरीकों को आज़माते समय उपयोगी है।

Note: ज़्यादातर code editors आपको keyboard shortcut से चयनित block को comment out करने देते हैं, जिससे परीक्षण के दौरान यह तकनीक लागू करना तेज़ हो जाता है।
Warning: Remember to remove or restore commented-out code once you're done testing, leaving it in permanently makes the file confusing for others.

उदाहरण: Commenting Out Code

javascript
console.log("This runs");
// console.log("This is disabled for testing");

उपयोगी Comments लिखना

सबसे मूल्यवान comments यह समझाते हैं कि code का कोई टुकड़ा क्यों मौजूद है या कोई विशेष तरीका क्यों चुना गया, क्योंकि code खुद पहले से दिखाता है कि वह क्या करता है, comments को वह संदर्भ जोड़ना चाहिए जो अकेले पढ़ने से स्पष्ट नहीं होता।

Note: comment लिखने से पहले पूछिए कि क्या वह ऐसी चीज़ समझाता है जो code पहले से साफ़ नहीं करता, अगर नहीं, तो वह ग़ैरज़रूरी हो सकता है।
Warning: Comments that simply restate the code, like '// set x to 5' above 'const x = 5;', add clutter without adding real value.

उदाहरण: Writing Useful Comments

javascript
// Using 0.9 because the API rounds up otherwise
const threshold = 0.9;
console.log(threshold);

JSDoc की बुनियाद

JSDoc एक संरचित comment शैली है, जो /** से शुरू होती है, और किसी function के उद्देश्य, parameters और return value को मानकीकृत रूप में दस्तावेज़ित करती है, जिसे कई editors और tools पढ़कर उपयोगी संकेत दे सकते हैं।

Note: किसी function के ऊपर एक सरल JSDoc comment भी आपके editor के autocomplete सुझावों को कहीं ज़्यादा मददगार बना सकता है।
Warning: JSDoc comments must start with exactly /** (two asterisks) to be recognized by tools, a single /* is treated as a regular comment.

उदाहरण: JSDoc Basics

javascript
/**
 * Adds two numbers together.
 * @param {number} a
 * @param {number} b
 * @returns {number}
 */
function add(a, b) {
  return a + b;
}
console.log(add(2, 3));
Live Example
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. ऐसे comments लिखना जो सिर्फ यह दोहराते हैं कि code obviously क्या करता है, यह बताने के बजाय कि वह ऐसा क्यों करता है।
  2. किसी multi-line comment को */ से close करना भूल जाना, जो अनजाने में बाद के सारे code को comment out कर देता है।
  3. बिना इस्तेमाल के purane, commented-out code के बड़े blocks को file में हमेशा के लिए छोड़ देना, unused code delete करने के बजाय उसे बिखेर देना।
चैप्टर सारांश
  • Single-line comments // से शुरू होते हैं, और उस line पर उनके बाद सब कुछ नजरअंदाज कर दिया जाता है।
  • Multi-line comments /* और */ में wrap होते हैं, और जितनी लाइनें चाहिए उतनी span कर सकते हैं।
  • Comments tricky logic explain करने और testing के दौरान code temporarily disable करने के लिए उपयोगी हैं।
ब्राउज़र सपोर्ट

// और /* */ दोनों comment syntax core JavaScript का हिस्सा हैं और हर browser और JavaScript engine द्वारा समान रूप से supported हैं।

🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 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.