JavaScript String Concatenation
JavaScript String Concatenation అంటే ఏమిటి?
రెండు లేదా అంతకంటే ఎక్కువ strings ను కలిపి ఒక కొత్త string తయారు చేయడాన్ని String Concatenation అంటారు. JavaScript లో strings ను కలపడానికి సాధారణంగా + operator ఉపయోగిస్తాము.
+ operator తో strings కలపడం
ఒక string మరియు మరొక string మధ్య + operator ఉంచితే అవి కలుస్తాయి.
ఉదాహరణకు, “Hello, ” + “Ravi” ఫలితం “Hello, Ravi” అవుతుంది.
String మరియు number కలిపినప్పుడు
ఒకవైపు string, మరొకవైపు number ఉంటే + operator సాధారణంగా number ను string గా మార్చి concatenation చేస్తుంది.
“Age: ” + 25 ఫలితం “Age: 25”. ఇది addition కాదు; ఇది string concatenation.
Explicit type conversion
Conversion ను మనమే స్పష్టంగా చేయాలంటే String() function ఉపయోగించవచ్చు. ఉదాహరణకు, String(25) ఫలితం “25” అనే string.
Numbers ను కలపాలనుకుంటే values strings గా మారకుండా జాగ్రత్తపడాలి. 10 + 5 ఫలితం 15, కానీ “10” + 5 ఫలితం “105”.
Template literals
చాలా values ను readable గా కలపడానికి backticks తో template literals ఉపయోగించవచ్చు. ${name} వంటి expression ను string లో embed చేయవచ్చు.
Simple Explanation
String concatenation అంటే strings ను కలపడం. JavaScript లో + operator string తో ఉపయోగించినప్పుడు values ను పక్కపక్కన కలుపుతుంది. Number ను string గా మార్చాలంటే String() ఉపయోగించవచ్చు.
- “Hi ” + “there” → “Hi there”
- “Total: ” + 10 → “Total: 10”
- 10 + 5 → 15
- “10” + 5 → “105”
అందువల్ల + operator addition చేస్తుందా లేదా concatenation చేస్తుందా అనేది operands యొక్క data types పై ఆధారపడి ఉంటుంది.
Real-Life Example
ఒక online shopping website లో customer పేరు మరియు order number ను కలిపి message చూపించాలి అనుకోండి. పేరు “Sita”, order number 245 అయితే “Sita, your order number is 245” అనే message తయారు చేయవచ్చు.
ఇక్కడ పేరు మరియు message strings, order number మాత్రం number కావచ్చు. + operator లేదా template literal ఉపయోగించి వీటిని ఒక readable message గా కలపవచ్చు.
Code Examples
Strings మరియు number తో concatenation
const firstName = "Ravi";
const age = 25;
const message = "My name is " + firstName + " and I am " + age + " years old.";
console.log(message);
ఈ ఉదాహరణలో firstName ఒక string, age ఒక number. + operator వీటిని message గా కలుపుతుంది. String భాగాలతో number కలుస్తున్నందున age text రూపంలో message లో చేరుతుంది.
My name is Ravi and I am 25 years old.
Explicit String conversion
const score = 95;
const scoreText = String(score);
console.log("Score: " + scoreText);
console.log(typeof scoreText);
String(score) ద్వారా number అయిన score ను string గా మార్చాము. తరువాత దాన్ని message తో concatenate చేశాము. typeof ద్వారా result యొక్క type string అని తెలుసుకోవచ్చు.
Score: 95 string
Template literal తో values కలపడం
const product = "Notebook";
const price = 50;
const bill = `Product: ${product}, Price: ${price}`;
console.log(bill);
Backticks తో రాసిన template literal లో ${product} మరియు ${price} expressions values ను string లోకి insert చేస్తాయి. ఎక్కువ values ఉన్నప్పుడు ఇది + concatenation కంటే readable గా ఉంటుంది.
Product: Notebook, Price: 50
Line-by-Line Explanation
firstName అనే constant variable లో “Ravi” string value ను store చేస్తున్నాము.
age variable లో 25 అనే number value ను store చేస్తున్నాము.
ఈ empty line code sections ను visually separate చేయడానికి మాత్రమే ఉపయోగించబడింది.
ఇక్కడ + operator strings మరియు variables ను వరుసగా కలుపుతుంది. age number అయినప్పటికీ, అది string భాగాలతో కలుస్తున్నందున text రూపంలో message లో చేరుతుంది.
message variable లో ఉన్న పూర్తి concatenated string ను console లో చూపిస్తుంది.
Try It
Try this JavaScript code:
const studentName = "Anu";
const marks = 88;
const result = "Student: " + studentName + ", Marks: " + marks;
console.log(result);
Practice
Practice:
productName, quantity, మరియు price variables ను ఉపయోగించి order message తయారు చేయండి. Output format ఇలా ఉండాలి: Product: Pen, Quantity: 3, Total: 60.
Hint:
ముందుగా quantity * price తో total calculate చేయండి. తరువాత + operator ఉపయోగించి అన్ని values ను message గా కలపండి.
Common Mistakes
“10” + 5 ఫలితం 15 వస్తుందని అనుకోవడం.
ఇక్కడ మొదటి value string కాబట్టి result “105” అవుతుంది. Addition కావాలంటే string ను number గా మార్చాలి; ఉదాహరణకు Number(“10”) + 5.
Message లో spaces ఇవ్వకుండా strings ను concatenate చేయడం.
Words మధ్య అవసరమైన spaces ను string లోనే ఇవ్వండి. ఉదాహరణకు “Hello, ” + name లో comma తరువాత space ఉంది.
String మరియు number మధ్య తేడా గుర్తించకుండా values ను కలపడం.
Value type తెలుసుకోవడానికి typeof ఉపయోగించండి. అవసరమైతే String() లేదా Number() తో explicit conversion చేయండి.
చాలా పొడవైన + concatenation ను unreadable గా రాయడం.
చాలా values ఉన్నప్పుడు template literals ఉపయోగిస్తే code మరింత readable గా ఉంటుంది.
Interview Questions
JavaScript లో string concatenation అంటే ఏమిటి?
Answer:
రెండు లేదా అంతకంటే ఎక్కువ strings ను కలిపి ఒక కొత్త string తయారు చేయడాన్ని string concatenation అంటారు. సాధారణంగా + operator ఉపయోగిస్తారు.
“5” + 2 మరియు 5 + 2 ఫలితాలు ఏమిటి?
Answer:
“5” + 2 ఫలితం “52”, ఎందుకంటే string తో concatenation జరుగుతుంది. 5 + 2 ఫలితం 7, ఎందుకంటే రెండు operands numbers.
String() function ను ఎందుకు ఉపయోగిస్తారు?
Answer:
ఏదైనా value ను string గా explicit గా మార్చడానికి String() function ఉపయోగిస్తారు. ఉదాహరణకు String(100) ఫలితం “100”.
String concatenation కోసం template literals యొక్క ప్రయోజనం ఏమిటి?
Answer:
Template literals లో backticks మరియు ${} syntax ఉపయోగించి variables ను నేరుగా string లో insert చేయవచ్చు. ఇది పొడవైన concatenation కంటే readable గా ఉంటుంది.
Exam Questions
“Hello” + ” World” expression యొక్క output ఏమిటి?
Answer:
Output “Hello World”. రెండు strings + operator తో concatenate అవుతాయి.
const result = “Total: ” + 25; లో result యొక్క value మరియు type ఏమిటి?
Answer:
Value “Total: 25”, type string. String తో number కలిసినందున concatenation జరుగుతుంది.
Number value ను string గా మార్చడానికి ఏ function ఉపయోగిస్తారు?
Answer:
String() function ఉపయోగిస్తారు. ఉదాహరణకు String(25) ఒక string value ను ఇస్తుంది.
Quick Revision
- String concatenation అంటే strings ను కలపడం.
- Strings ను కలపడానికి సాధారణంగా + operator ఉపయోగిస్తారు.
- String మరియు number ను + తో కలిపితే number సాధారణంగా string గా మారుతుంది.
- “10” + 5 ఫలితం “105”, కానీ 10 + 5 ఫలితం 15.
- Explicit string conversion కోసం String() ఉపయోగించండి.
- చాలా values ను readable గా కలపడానికి template literals ఉపయోగించవచ్చు.
