📘 Lesson · Lesson 05
Comments in JavaScript
JavaScript में Comments
Single-line Comments — //
// This is a single-line comment
let price = 100; // comments can also go at the end of a line
// console.log(price); ← this line won't run, it's commented
A single-line comment starts with two forward slashes
// — everything after them on that line is ignored by JavaScript. It's a note for humans only. This is the most common comment type: use it to explain a tricky line, label a section, or leave yourself a reminder. You can put a // comment on its own line, or at the END of a line of code (after the actual statement). Recall that CSS did NOT have // comments (it only used /* */) — JavaScript, however, fully supports this handy single-line style.Multi-line Comments — /* */
/* This is a multi-line comment.
It can span across
as many lines as you need. */
/*
Often used for longer explanations
or a block of notes at the top of a file.
*/
For comments spanning multiple lines, use
/* to start and */ to end — everything between them is ignored, across as many lines as you like. This is the same syntax CSS used! It's handy for longer explanations, a description block at the top of a file, or temporarily disabling several lines at once. So JavaScript gives you BOTH comment styles: // for quick single-line notes (the one you'll use most), and /* */ for multi-line blocks. Having both makes commenting flexible.Why Use Comments
- Explain the "why": the code shows WHAT it does; a comment explains WHY you did it that way.
- Label sections: mark where different parts of your code begin, so it's easy to navigate.
- Notes to your future self: weeks later you'll forget your own reasoning — a comment saves you.
- Help teammates: others reading your code understand your intent faster.
Just like in HTML and CSS, comments make code understandable. The best comments explain reasoning that isn't obvious from the code itself — // use timeout to wait for the animation to finish is far more useful than // set a timeout (which just repeats what the code already says).
Commenting Out Code — the debugging superpower
let total = price * quantity;
// console.log(total); ← temporarily disabled, not deleted
sendOrder(total);
/* Disable a whole block while testing:
sendEmail();
updateDatabase();
*/
Just like in CSS, "commenting out" code is a daily, practical use of comments. When you want to temporarily stop a line (or block) from running — to test something, or to isolate a bug — you don't delete it; you comment it out. Add
// before a single line, or wrap several lines in /* */. The code stays safely in your file, ready to re-enable by removing the comment marks. This is one of the core techniques for debugging: disable parts of your code to narrow down which part is causing a problem. You'll do this constantly.Good Comment Habits
- Comment the why, not the obvious:
// x = x + 1as a comment is useless noise; explain the purpose instead. - Keep them updated: an outdated comment that contradicts the code is worse than no comment — update comments when you change code.
- Don't over-comment: clean, well-named code needs fewer comments. If every line needs a comment, the code itself may be too confusing.
- Use them to plan: some developers write comments first as a to-do outline, then fill in the code beneath each one.
Comments are a tool for clarity, not decoration. A few well-placed, meaningful comments beat dozens of obvious ones. As you write more JavaScript, you'll develop a feel for when a comment genuinely helps — usually when the code does something clever, non-obvious, or important that a future reader (including you) would want explained.
Exam Corner
Q: How do you write a single-line comment? With
Q: How do you write a multi-line comment? With
Q: Does JavaScript support // comments (unlike CSS)? Yes — JavaScript has both
Q: What is "commenting out" code? Temporarily disabling code by turning it into a comment, instead of deleting it — used for debugging.
Q: What should a good comment explain? The "why" (the reasoning), not the obvious "what" the code already shows.
// — everything after it on the line is ignored.Q: How do you write a multi-line comment? With
/* to start and */ to end.Q: Does JavaScript support // comments (unlike CSS)? Yes — JavaScript has both
// and /* */; CSS only had /* */.Q: What is "commenting out" code? Temporarily disabling code by turning it into a comment, instead of deleting it — used for debugging.
Q: What should a good comment explain? The "why" (the reasoning), not the obvious "what" the code already shows.
Single-line Comments — //
// Yeh single-line comment hai
let price = 100; // comments line ke ant me bhi aa sakte hain
// console.log(price); ← yeh line nahi chalegi, comment ho gayi
Single-line comment दो forward slashes
// से शुरू होता है — उस line पर उनके बाद सब कुछ JavaScript ignore करता है. यह सिर्फ इंसानों के लिए नोट है. यह सबसे common comment प्रकार है: किसी पेचीदा line को समझाने, section label करने, या खुद को याद दिलाने को use कीजिए. आप // comment अपनी line पर, या code की line के ANT में (असली statement के बाद) रख सकते हैं. याद कीजिए CSS में // comments NAHI थे (सिर्फ /* */) — JavaScript, हालांकि, इस काम की single-line style को पूरा support करता है.Multi-line Comments — /* */
/* Yeh multi-line comment hai.
Yeh jitni lines chahiye
utni tak fail sakta hai. */
/*
Aksar lambi vyakhyaon ke liye
ya file ke ooper notes ke block ke liye.
*/
कई lines में फैले comments के लिए, शुरू करने को
/* और खत्म करने को */ use कीजिए — उनके बीच सब कुछ ignore होता है, जितनी चाहें lines तक. यह वही syntax है जो CSS use करता था! यह लंबी व्याख्याओं, file के ऊपर description block, या एक साथ कई lines अस्थायी रूप से बंद करने को काम का है. तो JavaScript आपको DONO comment styles देता है: जल्दी single-line notes के लिए // (जो आप सबसे ज़्यादा use करेंगे), और multi-line blocks के लिए /* */. दोनों होना commenting को लचीला बनाता है.Comments क्यों use करें
- "क्यों" समझाइए: code दिखाता है वह KYA करता है; comment समझाता है आपने ऐसा KYUN किया.
- Sections label कीजिए: चिन्हित कीजिए आपके code के अलग हिस्से कहां शुरू होते हैं, ताकि navigate करना आसान हो.
- भविष्य के खुद के लिए notes: हफ्तों बाद आप अपना ही तर्क भूल जाएंगे — comment आपको बचाता है.
- साथियों की मदद: आपका code पढ़ने वाले दूसरे आपका इरादा तेज़ी से समझते हैं.
HTML और CSS की तरह, comments code को समझने योग्य बनाते हैं. सबसे अच्छे comments वह तर्क समझाते हैं जो code से खुद स्पष्ट नहीं — // animation khatm hone ka intezaar karne ko timeout // timeout set karo (जो बस दोहराता है जो code पहले से कहता है) से कहीं ज़्यादा उपयोगी है.
Code Comment Out करना — debugging superpower
let total = price * quantity;
// console.log(total); ← asthayi roop se band, delete nahi
sendOrder(total);
/* Test karte samay poora block band karo:
sendEmail();
updateDatabase();
*/
CSS की तरह, "commenting out" code comments का रोज़मर्रा, व्यावहारिक इस्तेमाल है. जब आप किसी line (या block) को अस्थायी रूप से चलने से रोकना चाहें — कुछ test करने को, या bug अलग करने को — आप उसे delete नहीं करते; comment out करते हैं. एक line से पहले
// जोड़िए, या कई lines को /* */ में लपेटिए. Code आपकी file में सुरक्षित रहता है, comment चिन्ह हटाकर फिर चालू करने को तैयार. यह debugging की core तकनीकों में से एक है: अपने code के हिस्से बंद करके संकुचित कीजिए कौन-सा हिस्सा problem कर रहा है. आप यह लगातार करेंगे.अच्छी Comment आदतें
- "क्यों" comment कीजिए, स्पष्ट नहीं: comment के रूप में
// x = x + 1बेकार शोर है; बजाय मकसद समझाइए. - उन्हें update रखिए: पुराना comment जो code का खंडन करे बिना comment से बुरा है — code बदलने पर comments update कीजिए.
- ज़्यादा comment मत कीजिए: साफ, अच्छे नाम वाले code को कम comments चाहिए. अगर हर line को comment चाहिए, शायद code ही बहुत उलझाने वाला है.
- Planning के लिए use कीजिए: कुछ developers पहले comments को to-do outline के रूप में लिखते हैं, फिर हर एक के नीचे code भरते हैं.
Comments स्पष्टता का tool हैं, सजावट नहीं. कुछ अच्छी जगह रखे, अर्थपूर्ण comments दर्जनों स्पष्ट वालों से बेहतर हैं. जैसे-जैसे आप ज़्यादा JavaScript लिखेंगे, आपको अंदाज़ा होगा कब comment सच में मदद करता है — आमतौर पर जब code कुछ चतुर, गैर-स्पष्ट, या ज़रूरी करता है जिसे भविष्य का पाठक (आप भी) समझाया जाना चाहेगा.
Exam Corner
Q: Single-line comment कैसे लिखते हैं?
Q: Multi-line comment कैसे लिखते हैं?
Q: क्या JavaScript // comments support करता है (CSS के उलट)? हां — JavaScript में
Q: "Commenting out" code क्या है? Delete करने के बजाय code को comment बनाकर अस्थायी रूप से बंद करना — debugging के लिए.
Q: अच्छे comment को क्या समझाना चाहिए? "क्यों" (तर्क), न कि स्पष्ट "क्या" जो code पहले से दिखाता है.
// से — उस line पर उसके बाद सब ignore होता है.Q: Multi-line comment कैसे लिखते हैं?
/* से शुरू और */ से खत्म.Q: क्या JavaScript // comments support करता है (CSS के उलट)? हां — JavaScript में
// और /* */ दोनों हैं; CSS में सिर्फ /* */ था.Q: "Commenting out" code क्या है? Delete करने के बजाय code को comment बनाकर अस्थायी रूप से बंद करना — debugging के लिए.
Q: अच्छे comment को क्या समझाना चाहिए? "क्यों" (तर्क), न कि स्पष्ट "क्या" जो code पहले से दिखाता है.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का
console.log देखने के लिए F12 दबाइए.