JavaScript Introduction Practice
JavaScript Introduction Practice
ఈ lesson లో JavaScript యొక్క basic concepts ను practice చేస్తాము. JavaScript ను ఉపయోగించి messages చూపించడం, values ను calculate చేయడం మరియు simple decisions తీసుకోవడం నేర్చుకుంటాము.
JavaScript code ఎలా run అవుతుంది?
JavaScript code ను browser console, Node.js environment లేదా JavaScript editor లో run చేయవచ్చు. ఈ lesson లో examples console output ఆధారంగా ఉంటాయి.
ముఖ్యమైన basic concepts
- console.log() ద్వారా output ను చూపవచ్చు.
- const మరియు let ద్వారా values ను store చేయవచ్చు.
- Arithmetic operators తో calculations చేయవచ్చు.
- if…else ద్వారా condition ఆధారంగా నిర్ణయం తీసుకోవచ్చు.
Practice విధానం
- మొదట code ను జాగ్రత్తగా చదవండి.
- ప్రతి line ఏం చేస్తుందో అర్థం చేసుకోండి.
- Code ను editor లో run చేసి output ను పరిశీలించండి.
- తర్వాత values మార్చి మళ్లీ run చేయండి.
Simple Explanation
JavaScript ను ఒక smart assistant లాగా ఊహించుకోవచ్చు. మీరు ఇచ్చిన instructions ఆధారంగా అది message చూపిస్తుంది, numbers ను calculate చేస్తుంది మరియు conditions ను check చేస్తుంది.
ఉదాహరణకు, const price = 100; అంటే price అనే పేరు కింద 100 value ను store చేయడం. console.log(price); అంటే ఆ value ను output గా చూపించడం.
ఈ practice lesson లో చిన్న చిన్న code statements ను కలిపి ఒక complete program తయారు చేస్తాము.
Real-Life Example
ఒక shopping app లో product price మరియు quantity ఉంటే, JavaScript వాటి ఆధారంగా total price ను calculate చేయగలదు. అలాగే total price ఒక limit కంటే ఎక్కువగా ఉంటే discount message చూపించవచ్చు.
ఇది మన practice లోని rectangle area calculation లాంటిదే. Input values తీసుకుని, expression ద్వారా result తయారు చేసి, చివరలో output చూపిస్తాము.
Code Examples
Message మరియు calculation practice
const learnerName = "Ravi";
const firstNumber = 10;
const secondNumber = 5;
const total = firstNumber + secondNumber;
console.log("Hello, " + learnerName);
console.log("Total: " + total);
మొదటి line లో learnerName అనే constant లో పేరు store అవుతుంది. తరువాత రెండు numbers ను store చేసి, + operator తో total calculate చేస్తాము. చివరగా రెండు messages ను console లో చూపిస్తాము.
+ operator numbers ను add చేయడానికి మరియు strings ను కలపడానికి ఉపయోగించబడింది.
Hello, Ravi Total: 15
Condition ఆధారంగా message
const score = 75;
if (score >= 50) {
console.log("Pass");
} else {
console.log("Try again");
}
score value 75 కాబట్టి score >= 50 condition true అవుతుంది. అందువల్ల if block లోని message output అవుతుంది. Condition false అయితే else block execute అవుతుంది.
Pass
Line-by-Line Explanation
ఈ line లో score అనే constant ను create చేసి, దానికి 75 value ఇస్తుంది.
ఈ ఖాళీ line code ను readable గా ఉంచడానికి మాత్రమే ఉపయోగించబడింది. దీనివల్ల program output పై ప్రభావం ఉండదు.
ఇక్కడ score value 50 కంటే greater than లేదా equal to ఉందా అని check చేస్తుంది. Condition true అయితే opening brace లోని code run అవుతుంది.
Condition true అయినప్పుడు ఈ line console లో Pass అనే text ను చూపిస్తుంది. రెండు spaces indentation code structure ను స్పష్టంగా చూపించడానికి ఉపయోగించబడింది.
మొదటి condition false అయినప్పుడు else block ప్రారంభమవుతుంది.
score 50 కంటే తక్కువగా ఉంటే ఈ line console లో Try again అనే message ను చూపిస్తుంది.
ఈ closing brace if…else statement ముగిసిందని సూచిస్తుంది.
Try It
Try this JavaScript code:
const length = 8;
const width = 5;
// Create the rectangle area here.
console.log("Rectangle area: " + area);
Practice
Practice:
Task: length మరియు width values ను ఉపయోగించి rectangle area ను calculate చేయండి. Area కోసం area అనే variable ను create చేసి, output గా Rectangle area: 40 చూపించండి.
Hint:
Rectangle area formula: length × width. JavaScript లో multiplication కోసం * operator ఉపయోగించండి.
Practice:
Practice code లో length ను 10 మరియు width ను 4 గా మార్చి program run చేయండి. కొత్త output ఏమిటో ముందుగా ఊహించండి.
Hint:
మొదట area formula ను apply చేయండి: 10 * 4.
Common Mistakes
area variable ను create చేయకుండానే console.log() లో ఉపయోగించడం.
ముందుగా const area = length * width; వంటి statement తో area value ను calculate చేయండి. తరువాత దాన్ని output చేయండి.
Multiplication కోసం x లేదా × symbol ఉపయోగించడం.
JavaScript లో multiplication కోసం keyboard లోని asterisk symbol అయిన * ను ఉపయోగించాలి.
String మరియు variable ను కలుపుతున్నప్పుడు + operator మర్చిపోవడం.
console.log(“Rectangle area: ” + area); లాగా string తరువాత + operator పెట్టండి.
Variable name ను create చేసినప్పుడు ఒక పేరు, ఉపయోగించినప్పుడు మరో spelling ఉపయోగించడం.
Variable name exact గా ఒకేలా ఉండాలి. ఉదాహరణకు area ను create చేస్తే, output లో కూడా area నే ఉపయోగించాలి.
Interview Questions
JavaScript లో console.log() ఎందుకు ఉపయోగిస్తారు?
Answer:
Values, messages లేదా program results ను console లో చూపించడానికి console.log() ఉపయోగిస్తారు. ఇది debugging మరియు learning సమయంలో చాలా ఉపయోగకరంగా ఉంటుంది.
const keyword ఉపయోగం ఏమిటి?
Answer:
తర్వాత మార్చకూడని value కోసం const ఉపయోగిస్తారు. ఉదాహరణకు const length = 8; లో length అనే binding ను create చేస్తాము.
JavaScript లో multiplication operator ఏది?
Answer:
Multiplication కోసం asterisk symbol * ఉపయోగిస్తారు. ఉదాహరణకు const area = length * width;.
if…else statement ఎలా పనిచేస్తుంది?
Answer:
మొదట if condition check అవుతుంది. Condition true అయితే if block run అవుతుంది. Condition false అయితే else block run అవుతుంది.
Exam Questions
ఈ code output ఏమిటి? const a = 6; const b = 4; console.log(a + b);
Answer:
Output 10. ఎందుకంటే + operator రెండు numbers ను add చేస్తుంది.
Rectangle area calculate చేయడానికి సరైన JavaScript statement ఏది?
Answer:
const area = length * width; అనేది సరైన statement.
ఈ condition ఎప్పుడు true అవుతుంది: score >= 50?
Answer:
score value 50 లేదా 50 కంటే ఎక్కువగా ఉన్నప్పుడు condition true అవుతుంది.
JavaScript code లో comments ఎందుకు ఉపయోగిస్తారు?
Answer:
Code purpose లేదా instructions ను explain చేయడానికి comments ఉపయోగిస్తారు. Comments program execution లో భాగం కావు.
Quick Revision
- console.log() output చూపించడానికి ఉపయోగిస్తారు.
- const ద్వారా constant binding ను create చేయవచ్చు.
- + addition మరియు string concatenation కోసం ఉపయోగించవచ్చు.
- * multiplication కోసం ఉపయోగిస్తారు.
- if…else condition ఆధారంగా code ను execute చేస్తుంది.
- Code ను చిన్న steps గా విడగొట్టి, ప్రతి result ను test చేయడం మంచి practice.
