JavaScript Comments
JavaScript Comments అంటే ఏమిటి?
JavaScript code లో developer కోసం రాసే notes ను comments అంటారు. JavaScript comments ను execute చేయదు. అంటే comments లో ఉన్న text output లో కనిపించదు మరియు program behavior పై ప్రభావం చూపదు.
Comments ఎందుకు ఉపయోగించాలి?
- Code ఏం చేస్తుందో సులభంగా అర్థం చేసుకోవడానికి.
- ముఖ్యమైన logic గురించి చిన్న note రాయడానికి.
- Debugging సమయంలో code లోని ఒక భాగాన్ని తాత్కాలికంగా ఆపడానికి.
- Team members తో code ఉద్దేశాన్ని పంచుకోవడానికి.
Single-line Comment
ఒకే line comment కోసం రెండు forward slashes ఉపయోగించాలి: //. ఈ symbols తర్వాత ఉన్న text ఆ line చివరి వరకు comment గా పరిగణించబడుతుంది.
Multi-line Comment
అనేక lines కు comment రాయడానికి /* తో ప్రారంభించి */ తో ముగించాలి. ఈ markers మధ్య ఉన్న మొత్తం text JavaScript చేత ignore చేయబడుతుంది.
మంచి Comments ఎలా రాయాలి?
Comment code లో ఇప్పటికే స్పష్టంగా కనిపించే విషయాన్ని మళ్లీ చెప్పకుండా, code వెనుక ఉన్న కారణాన్ని వివరించాలి. చాలా ఎక్కువ comments రాస్తే code చదవడం కష్టమవుతుంది. అవసరమైన చోట చిన్నగా, స్పష్టంగా రాయండి.
Simple Explanation
Comments అనేవి JavaScript program లో మనం రాసే explanatory notes. Browser లేదా JavaScript engine వాటిని run చేయదు. Single-line comment కోసం // ఉపయోగిస్తాం. Multi-line comment కోసం /* మరియు */ ఉపయోగిస్తాం.
ఉదాహరణకు, // Display the name అనేది developer కోసం మాత్రమే. ఈ text console లో output గా కనిపించదు.
Real-Life Example
ఒక recipe పుస్తకంలో ప్రతి step పక్కన chef చిన్న notes రాసినట్లు comments పని చేస్తాయి. ఆ notes recipe లోని ingredients కావు, కానీ recipe అర్థం చేసుకోవడానికి సహాయపడతాయి. అలాగే JavaScript comments program లో execute కావు, కానీ developers కు code అర్థం చేసుకోవడంలో సహాయపడతాయి.
Code Examples
Single-line మరియు Multi-line Comments
const name = "Ravi";
// This line prints the learner's name
console.log(name);
/*
This is a multi-line comment.
It is ignored by JavaScript.
*/
console.log("Comments help readability.");
ఇక్కడ // తో మొదలైన line ఒక single-line comment. /* మరియు */ మధ్య ఉన్న lines multi-line comment. ఈ comments execute కావు. అందువల్ల console లో రెండు console.log() statements యొక్క output మాత్రమే కనిపిస్తుంది.
Ravi Comments help readability.
Debugging కోసం Code ను తాత్కాలికంగా Comment చేయడం
const firstNumber = 10;
const secondNumber = 5;
console.log(firstNumber + secondNumber);
// console.log(firstNumber - secondNumber);
మొదటి console.log() addition result ను చూపిస్తుంది. రెండవ console.log() ముందు // ఉండటం వల్ల అది comment గా మారింది మరియు execute కాదు. Debugging సమయంలో code ను delete చేయకుండా తాత్కాలికంగా ఆపడానికి ఈ విధానం ఉపయోగపడుతుంది.
15
Line-by-Line Explanation
ఈ line లో name అనే constant లో Ravi అనే string value నిల్వ చేయబడుతుంది.
// తో మొదలైన ఈ line single-line comment. JavaScript దీన్ని execute చేయదు; ఇది developer కు వివరణగా మాత్రమే ఉంటుంది.
ఈ line name లో ఉన్న value ను console లో చూపిస్తుంది. అందుకే output గా Ravi కనిపిస్తుంది.
ఈ empty line code ను చదవడం సులభంగా చేయడానికి ఉపయోగపడుతుంది. దీనివల్ల program output మారదు.
ఈ marker multi-line comment ప్రారంభాన్ని సూచిస్తుంది.
ఈ text comment లో భాగం. JavaScript దీన్ని code గా execute చేయదు.
ఈ line కూడా multi-line comment లోనే ఉంది కాబట్టి JavaScript దీనిని ignore చేస్తుంది.
ఈ marker multi-line comment ముగిసిందని సూచిస్తుంది.
ఈ executable line console లో Comments help readability. అనే text ను చూపిస్తుంది.
Try It
Try this JavaScript code:
const topic = "JavaScript Comments";
// Add a comment that explains the next line.
console.log(topic);
/*
Add a short multi-line comment here.
*/
console.log("Comments are useful.");
Practice
Practice:
పై code లో మొదటి console.log() ముందు project name ను వివరించే single-line comment రాయండి. రెండవ console.log() ముందు కనీసం రెండు lines ఉన్న multi-line comment జోడించండి. Comments వల్ల output మారకూడదు.
Hint:
Single-line comment కోసం // ఉపయోగించండి. Multi-line comment కోసం /* తో ప్రారంభించి */ తో ముగించండి.
Common Mistakes
Comment ప్రారంభించడానికి ఒకే slash, అంటే /, ఉపయోగించడం.
Single-line comment కోసం తప్పనిసరిగా రెండు slashes, అంటే //, ఉపయోగించాలి.
Multi-line comment కు */ closing marker ఇవ్వకుండా వదిలేయడం.
ప్రతి multi-line comment ను /* తో ప్రారంభించి */ తో తప్పనిసరిగా ముగించాలి.
Comment లో ఉన్న text console output లో కనిపిస్తుందని అనుకోవడం.
Comments JavaScript చేత execute కావు. అందువల్ల వాటి text output లో కనిపించదు.
Code లోని ప్రతి line కు అనవసరమైన comment రాయడం.
Code లో స్పష్టంగా కనిపించని కారణం లేదా ముఖ్యమైన logic ఉన్నప్పుడు మాత్రమే చిన్న, meaningful comment రాయండి.
Interview Questions
JavaScript లో comment అంటే ఏమిటి?
Answer:
JavaScript engine execute చేయని explanatory text ను comment అంటారు. ఇది code అర్థం చేసుకోవడానికి developers కు సహాయపడుతుంది.
Single-line comment కోసం ఏ syntax ఉపయోగిస్తారు?
Answer:
Single-line comment కోసం // ఉపయోగిస్తారు. ఆ symbols తర్వాత ఉన్న text line చివరి వరకు comment గా ఉంటుంది.
Multi-line comment ఎలా రాయాలి?
Answer:
Multi-line comment ను /* తో ప్రారంభించి */ తో ముగించాలి. ఈ markers మధ్య ఉన్న అనేక lines execute కావు.
Code ను delete చేయకుండా తాత్కాలికంగా ఆపడానికి comments ఎలా సహాయపడతాయి?
Answer:
Code line ముందు // ఉంచితే ఆ line comment గా మారుతుంది మరియు execute కాదు. Debugging తర్వాత // తొలగించి code ను మళ్లీ run చేయవచ్చు.
Exam Questions
క్రింది వాటిలో single-line comment కు సరైన syntax ఏది? A) /* note */ B) // note C) ## note D) — note
Answer:
సరైన సమాధానం: B) // note.
క్రింది code output ఏమిటి? console.log(“Start”); // console.log(“Middle”); console.log(“End”);
Answer:
Output రెండు lines గా ఉంటుంది: Start మరియు End. Comment లో ఉన్న console.log(“Middle”); execute కాదు.
Multi-line comment ప్రారంభ మరియు ముగింపు markers ను రాయండి.
Answer:
Multi-line comment /* తో ప్రారంభమై */ తో ముగుస్తుంది.
Quick Revision
-
Comments JavaScript చేత execute కావు.
-
Single-line comment కోసం // ఉపయోగిస్తారు.
-
Multi-line comment కోసం /* మరియు */ ఉపయోగిస్తారు.
-
Comments code readability మరియు debugging కు సహాయపడతాయి.
-
మంచి comment code వెనుక ఉన్న కారణాన్ని వివరిస్తుంది; స్పష్టంగా కనిపించే code ను మాత్రమే repeat చేయదు.
