JavaScript Boolean and Undefined
JavaScript Boolean మరియు Undefined
JavaScript లో data ను different data types రూపంలో store చేస్తాము. ఈ lesson లో Boolean మరియు Undefined data types గురించి నేర్చుకుంటాము.
Boolean అంటే ఏమిటి?
Boolean data type కు రెండు values మాత్రమే ఉంటాయి: true మరియు false. ఒక విషయం సరైందా కాదా, ఒక user login అయ్యాడా లేదా, ఒక option enable అయిందా లేదా వంటి పరిస్థితులను చూపడానికి Boolean ఉపయోగిస్తాము.
Boolean values ను quotes లో రాయకూడదు. true ఒక Boolean value, కానీ “true” ఒక String value.
Undefined అంటే ఏమిటి?
ఒక variable ను declare చేసి value assign చేయకపోతే, దాని default value undefined అవుతుంది.
undefined అంటే value ఇంకా ఇవ్వలేదు లేదా value అందుబాటులో లేదు అని అర్థం. ఇది ఒక ప్రత్యేక JavaScript data type value.
typeof operator
ఒక value ఏ data type కు చెందినదో తెలుసుకోవడానికి typeof operator ఉపయోగిస్తాము. Boolean value కోసం typeof result “boolean” అవుతుంది. Undefined value కోసం result “undefined” అవుతుంది.
Boolean values ను ఉపయోగించడం
Login status, payment status, permission status మరియు settings వంటి విషయాలను Boolean variables లో store చేయవచ్చు. ఉదాహరణకు isLoggedIn variable లో user login status ను store చేయవచ్చు.
Simple Explanation
Boolean అంటే yes లేదా no లాంటి రెండు possibilities లో ఒకటి. JavaScript లో ఇవి true మరియు false గా రాస్తాము.
Undefined అంటే variable ఉంది కానీ దానికి ఇంకా value assign చేయలేదు. ఉదాహరణకు let age; అని రాస్తే, age విలువ undefined అవుతుంది.
గుర్తుంచుకోండి: true మరియు false Boolean values; “true” మరియు “false” Strings.
Real-Life Example
ఒక school attendance app ను ఊహించండి. ఒక student ఈరోజు class కు వచ్చాడంటే isPresent = true, రాకపోతే isPresent = false అని store చేయవచ్చు.
అదే student phone number ఇంకా నమోదు చేయకపోతే, phoneNumber variable కు value assign చేయబడలేదు. అప్పుడు దాని value undefined అవుతుంది.
Code Examples
Boolean values ను store చేయడం
const isLoggedIn = true;
const hasPermission = false;
console.log(isLoggedIn);
console.log(hasPermission);
isLoggedIn లో true మరియు hasPermission లో false store చేశాము. ఇవి రెండూ Boolean values.
true false
Boolean మరియు Undefined యొక్క type తెలుసుకోవడం
const isAvailable = true;
let selectedItem;
console.log(typeof isAvailable);
console.log(typeof selectedItem);
isAvailable ఒక Boolean value కాబట్టి దాని type “boolean”. selectedItem కు value assign చేయలేదు కాబట్టి దాని value మరియు type undefined.
boolean undefined
Boolean value మరియు String value మధ్య తేడా
const firstValue = true;
const secondValue = "true";
console.log(typeof firstValue);
console.log(typeof secondValue);
Quotes లేకుండా ఉన్న true Boolean value. Quotes లో ఉన్న “true” String value. అందుకే typeof results వేర్వేరుగా వస్తాయి.
boolean string
Line-by-Line Explanation
isLoggedIn అనే constant variable ను create చేసి దానికి Boolean value true assign చేస్తున్నాము.
hasPermission variable లో permission లేదని సూచించడానికి Boolean value false assign చేస్తున్నాము.
ఈ blank line code ను చదవడం సులభంగా ఉండటానికి మాత్రమే ఉపయోగించబడింది. దీనివల్ల program execution పై ప్రభావం ఉండదు.
isLoggedIn variable లో ఉన్న true value ను console లో చూపిస్తుంది.
hasPermission variable లో ఉన్న false value ను console లో చూపిస్తుంది.
Try It
Try this JavaScript code:
const isOnline = true;
let unreadMessage;
console.log("Online:", isOnline);
console.log("Unread message:", unreadMessage);
Practice
Practice:
పై code ను పూర్తి చేయండి. isLoggedIn value, దాని type, userEmail value మరియు దాని type ను console లో print చేయండి.
Expected output లో false, boolean, undefined, undefined రావాలి.
Hint:
console.log() ను నాలుగు సార్లు ఉపయోగించండి. Type తెలుసుకోవడానికి typeof operator ను variable ముందు ఉపయోగించండి.
Common Mistakes
Boolean value ను String లాగా “true” లేదా “false” quotes లో రాయడం.
Boolean value కోసం quotes ఉపయోగించకండి. true మరియు false ను నేరుగా రాయండి. Quotes ఉపయోగిస్తే అది String అవుతుంది.
Value assign చేయని variable ను undefined కాకుండా empty String లేదా zero అని అనుకోవడం.
let value; అని declare చేస్తే దాని value undefined. Empty String కోసం “”, zero కోసం 0 ను ప్రత్యేకంగా assign చేయాలి.
Declare చేయని variable ను access చేసి అది undefined వస్తుందని అనుకోవడం.
Declare చేయని variable ను access చేస్తే సాధారణంగా ReferenceError వస్తుంది. Declare చేసి value assign చేయని variable మాత్రమే undefined అవుతుంది.
undefined ను quotes లో రాసి actual undefined value అని అనుకోవడం.
undefined మరియు “undefined” వేర్వేరు. మొదటిది undefined value; రెండవది String.
Interview Questions
JavaScript లో Boolean data type అంటే ఏమిటి?
Answer:
Boolean data type కు రెండు values ఉంటాయి: true మరియు false. Conditions, login status, permissions మరియు on/off states ను represent చేయడానికి ఇది ఉపయోగపడుతుంది.
ఒక variable కు value assign చేయకపోతే ఏమవుతుంది?
Answer:
Variable declare చేసి value assign చేయకపోతే దాని value undefined అవుతుంది. ఉదాహరణకు let result; తర్వాత result విలువ undefined.
true మరియు “true” మధ్య తేడా ఏమిటి?
Answer:
true Boolean value, కానీ “true” String value. typeof true result “boolean”, typeof “true” result “string”.
Boolean మరియు Undefined values యొక్క type తెలుసుకోవడానికి ఏ operator ఉపయోగిస్తారు?
Answer:
typeof operator ఉపయోగిస్తాము. Boolean కోసం “boolean”, undefined కోసం “undefined” result వస్తుంది.
Exam Questions
క్రింది code యొక్క output ఏమిటి? const value = false; console.log(typeof value);
Answer:
Output boolean. ఎందుకంటే false ఒక Boolean value.
క్రింది code యొక్క output ఏమిటి? let userName; console.log(userName);
Answer:
Output undefined. ఎందుకంటే userName variable కు value assign చేయలేదు.
క్రింది వాటిలో Boolean value ఏది? 1) “false” 2) false 3) “true” 4) “undefined”
Answer:
సరైన సమాధానం 2) false. Quotes లేని false మాత్రమే Boolean value.
Declare చేయని variable ను access చేయడం మరియు declare చేసి value ఇవ్వని variable ను access చేయడం మధ్య తేడా ఏమిటి?
Answer:
Declare చేయని variable ను access చేస్తే సాధారణంగా ReferenceError వస్తుంది. Declare చేసి value assign చేయని variable ను access చేస్తే undefined వస్తుంది.
Quick Revision
- Boolean data type కు true మరియు false అనే రెండు values ఉంటాయి.
- Quotes లేని true లేదా false Boolean values; quotes ఉన్నవి Strings.
- Declare చేసిన variable కు value assign చేయకపోతే దాని value undefined అవుతుంది.
- typeof operator తో value యొక్క data type తెలుసుకోవచ్చు.
- undefined మరియు “undefined” ఒకటి కాదు.
- Declare చేయని variable మరియు value assign చేయని variable వేర్వేరు పరిస్థితులు.
