Imagine leaving sticky notes on a shared project at work, explaining why a decision was made, warning a colleague about a tricky part, or temporarily crossing out a step you're not using right now.
Comments in JavaScript are exactly that, notes written into your code that the browser completely ignores when running the script, but that are incredibly valuable to any human reading the code later, including future you.
Good comments explain the why behind a decision, not just the what, and international teams working on projects like cookiescursor.com rely on clear comments to stay in sync across different time zones.
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));
{# 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. #}