Using const in JavaScript
JavaScript లో const ఉపయోగించడం
const అనేది JavaScript లో variable ని declare చేయడానికి ఉపయోగించే keyword. ఒకసారి const variable కి value assign చేసిన తరువాత, ఆ variable ని మళ్లీ వేరే value తో assign చేయలేము.
const syntax
Syntax: const variableName = value;
ఉదాహరణకు, const country = “India”; అని రాస్తే, country variable కి “India” value assign అవుతుంది. తరువాత country = “USA”; అని value మార్చడానికి ప్రయత్నిస్తే error వస్తుంది.
const యొక్క ముఖ్యమైన నియమాలు
- const variable ని declare చేసేటప్పుడు తప్పనిసరిగా value ఇవ్వాలి.
- ఒకసారి value assign చేసిన తరువాత అదే variable కి కొత్త value assign చేయలేము.
- Variable name ని మళ్లీ declare చేయడానికి కూడా వీలుండదు.
- మార్చకూడని reference లేదా value కోసం సాధారణంగా const ఉపయోగిస్తాము.
const మరియు let మధ్య తేడా
let తో declare చేసిన variable value ని మార్చవచ్చు. const తో declare చేసిన variable కి కొత్త value assign చేయలేము. కాబట్టి value మారదని తెలిసినప్పుడు const ఉపయోగించడం మంచి practice.
Simple Explanation
const అంటే constant declaration. ఒక box లో value పెట్టి, ఆ box కి తాళం వేసినట్లుగా ఊహించుకోండి. ఆ value ని తరువాత replace చేయలేము. ఉదాహరణ: const daysInWeek = 7; ఇక్కడ daysInWeek value 7 గా ఉంటుంది. దానికి మళ్లీ వేరే number assign చేయకూడదు.
const variable కి declaration సమయంలోనే initialization చేయాలి. అంటే const score; అని మాత్రమే రాస్తే సరైనది కాదు.
Real-Life Example
ఒక వారంలో రోజుల సంఖ్య ఎప్పుడూ 7 గానే ఉంటుంది. అందువల్ల దాన్ని JavaScript లో const daysInWeek = 7; గా store చేయవచ్చు. కానీ ఒక విద్యార్థి score మారవచ్చు కాబట్టి దానికి let అనుకూలంగా ఉంటుంది.
Code Examples
const తో fixed value ఉపయోగించడం
const siteName = "AiTeluguLo";
const lessonsCount = 100;
console.log(siteName);
console.log(lessonsCount);
ఈ example లో siteName మరియు lessonsCount variables ని const తో declare చేశాము. వాటి values ని మార్చకుండా console లో display చేస్తున్నాము.
AiTeluguLo 100
const variable ని మార్చలేము
const language = "JavaScript";
console.log(language);
ఇక్కడ language variable కి “JavaScript” value assign చేశాము. const ఉపయోగించినందువల్ల ఈ variable కి తరువాత కొత్త value assign చేయకూడదు. ఈ code ఎలాంటి error లేకుండా language value ని display చేస్తుంది.
JavaScript
Line-by-Line Explanation
const keyword తో daysInWeek అనే variable ని declare చేసి, దానికి 7 value assign చేస్తున్నాము.
ఇక్కడ message అనే variable లో string value “Learn JavaScript” ని store చేస్తున్నాము.
console.log() ద్వారా daysInWeek లో ఉన్న 7 value ని console లో చూపిస్తున్నాము.
ఈ line message variable లో ఉన్న text ని console లో display చేస్తుంది.
Try It
Try this JavaScript code:
const courseName = "JavaScript";
const totalModules = 5;
console.log(courseName);
console.log(totalModules);
Practice
Practice:
const ఉపయోగించి websiteName variable లో “AiTeluguLo” value మరియు lessonNumber variable లో 1 value ఉంచండి. తరువాత రెండు values ని console.log() తో display చేయండి.
Hint:
రెండు variables ని const తో declare చేయండి. తరువాత ప్రతి variable ని ఒక ప్రత్యేక console.log() statement లో ఉపయోగించండి.
Common Mistakes
const score; అని value assign చేయకుండా variable declare చేయడం.
const variable కి declaration సమయంలోనే value ఇవ్వాలి. సరైన విధానం: const score = 90;
ఒకసారి value assign చేసిన తరువాత const variable కి కొత్త value assign చేయడం.
const variable ని reassign చేయలేము. Value మారే అవకాశం ఉంటే let ఉపయోగించండి.
ఒకే scope లో అదే పేరుతో రెండు const variables ని declare చేయడం.
ప్రతి variable కి unique name ఇవ్వండి. ఒకే పేరు మళ్లీ అవసరమైతే కొత్త variable declare చేయకుండా ఉన్న variable ని ఉపయోగించండి.
Interview Questions
JavaScript లో const keyword ఎందుకు ఉపయోగిస్తారు?
Answer:
తరువాత మార్చకూడని variable binding కోసం const ఉపయోగిస్తారు. దీని ద్వారా accidental reassignment ను నివారించవచ్చు.
const variable ని declaration తరువాత reassign చేయవచ్చా?
Answer:
లేదు. const variable కి ఒకసారి value assign చేసిన తరువాత దానికి కొత్త value assign చేయలేము. ప్రయత్నిస్తే error వస్తుంది.
const variable ని value ఇవ్వకుండా declare చేయవచ్చా?
Answer:
లేదు. const declaration సమయంలోనే initialization అవసరం. ఉదాహరణ: const total = 10;
Exam Questions
క్రింది code లో సరైన const declaration ఏది?
- const age;
- const age = 20;
- const = 20;
Answer:
సరైన సమాధానం: const age = 20; ఎందుకంటే const variable కి declaration సమయంలో value assign చేయాలి.
const price = 50; తరువాత price = 60; రాస్తే ఏమి జరుగుతుంది?
Answer:
price ఒక const variable కాబట్టి దానికి కొత్త value assign చేయడం వల్ల error వస్తుంది.
Quick Revision
- const అనేది variable declaration keyword.
- const variable కి declaration సమయంలోనే value ఇవ్వాలి.
- const variable ని తరువాత reassign చేయలేము.
- Value మారని variables కోసం const ఉపయోగించడం మంచి practice.
- Value మారే variables కోసం let ఉపయోగించవచ్చు.
