JavaScript Null and Symbol
JavaScript లో Null మరియు Symbol
JavaScript లో null మరియు Symbol రెండు ప్రత్యేకమైన data types. ఇవి సాధారణ values కంటే భిన్నమైన సందర్భాల్లో ఉపయోగపడతాయి.
Null అంటే ఏమిటి?
null అంటే ఒక variable లో ప్రస్తుతం ఏ value లేదని మనం ఉద్దేశపూర్వకంగా సూచించడం. ఇది empty value లేదా intentional absence of value ను చూపిస్తుంది.
ఉదాహరణకు, ఒక user profile లో phone number ఇంకా ఇవ్వకపోతే ఆ variable కు null ఇవ్వవచ్చు.
undefined తో null ఒకటే కాదు. undefined అంటే value assign చేయలేదు లేదా value అందుబాటులో లేదు. null అంటే value లేదని developer ఉద్దేశపూర్వకంగా పెట్టాడు.
Null యొక్క ముఖ్యమైన విషయం
typeof null ఫలితం “object” వస్తుంది. ఇది JavaScript లో ఉన్న పాత historical behavior. null నిజంగా object కాదు, కానీ typeof operator ఈ ప్రత్యేకమైన ఫలితాన్ని ఇస్తుంది.
Symbol అంటే ఏమిటి?
Symbol అనేది unique మరియు immutable primitive value. ఒకే description తో రెండు Symbols సృష్టించినా అవి రెండూ వేర్వేరు values గా ఉంటాయి.
Symbol ను సాధారణంగా object properties కు unique keys ఇవ్వడానికి ఉపయోగిస్తారు. దీనివల్ల అదే పేరుతో ఉన్న ఇతర properties తో accidental conflict తగ్గుతుంది.
Symbol సృష్టించడానికి Symbol() ను ఉపయోగిస్తాం. ఉదాహరణకు Symbol(“id”) లోని “id” కేవలం description మాత్రమే; అది uniqueness ని నిర్ణయించదు.
Null మరియు Symbol ను గుర్తుంచుకోవడం
- null: value intentionally absent అని సూచిస్తుంది.
- Symbol: unique identifier లేదా unique key కోసం ఉపయోగపడుతుంది.
- ప్రతి కొత్త Symbol వేర్వేరు value ను సృష్టిస్తుంది.
- null యొక్క type ను typeof తో check చేస్తే “object” వస్తుంది.
Simple Explanation
null అంటే “ఇక్కడ value లేదు” అని మనమే స్పష్టంగా చెప్పడం. Symbol అంటే ప్రతి సారి కొత్తగా సృష్టించే unique value. రెండు Symbols కు ఒకే description ఉన్నా, అవి equal కావు.
Real-Life Example
ఒక online form లో phone number field ను user ఖాళీగా ఉంచితే, application ఆ value ను null గా ఉంచవచ్చు. ఒక apartment building లో ప్రతి ఇంటికి ప్రత్యేకమైన access key ఉన్నట్లు, object లో ప్రతి ప్రత్యేక property కోసం Symbol ను unique key గా ఉపయోగించవచ్చు.
Code Examples
Null value మరియు typeof null
const phoneNumber = null;
console.log(phoneNumber);
console.log(typeof phoneNumber);
ఇక్కడ phoneNumber కు value లేదని ఉద్దేశపూర్వకంగా null ఇచ్చాం. మొదటి console.log null ను చూపిస్తుంది. రెండవది JavaScript యొక్క historical behavior వల్ల “object” ను చూపిస్తుంది.
null object
రెండు Symbols unique values
const firstId = Symbol("id");
const secondId = Symbol("id");
console.log(firstId === secondId);
console.log(typeof firstId);
రెండు Symbols కు ఒకే description ఉన్నా, ప్రతి Symbol ప్రత్యేకంగా సృష్టించబడుతుంది. అందువల్ల strict equality comparison ఫలితం false. Symbol యొక్క type “symbol” గా వస్తుంది.
false symbol
Null మరియు undefined మధ్య తేడా
let plannedValue;
const clearedValue = null;
console.log(plannedValue);
console.log(clearedValue);
console.log(plannedValue === clearedValue);
plannedValue కు value assign చేయలేదు కాబట్టి అది undefined. clearedValue కు developer ఉద్దేశపూర్వకంగా null ఇచ్చాడు. రెండూ వేర్వేరు values కాబట్టి strict equality ఫలితం false.
undefined null false
Line-by-Line Explanation
ఈ line Symbol(“id”) ద్వారా ఒక unique Symbol value ను సృష్టించి, దానిని firstId constant లో store చేస్తుంది.
ఇదే description ఉపయోగించినా, ఈ line ఒక కొత్త మరియు వేర్వేరు Symbol value ను secondId లో store చేస్తుంది.
=== operator రెండు Symbols నిజంగా ఒకటేనా అని check చేస్తుంది. అవి వేర్వేరు Symbols కాబట్టి output false అవుతుంది.
typeof operator firstId యొక్క data type ను check చేస్తుంది. ఇది symbol అనే output ఇస్తుంది.
phoneNumber కు ప్రస్తుతం value లేదని developer ఉద్దేశపూర్వకంగా null assign చేస్తున్నాడు.
ఈ line phoneNumber యొక్క type ను చూపిస్తుంది. JavaScript historical behavior వల్ల output object వస్తుంది.
Try It
Try this JavaScript code:
const firstId = Symbol("user-id");
const secondId = Symbol("user-id");
console.log(firstId === secondId);
console.log(typeof firstId);
Practice
Practice:
Task: missingName అనే variable కు null value ఇవ్వండి. తరువాత రెండు వేర్వేరు Symbols ను user tokens గా సృష్టించి, వాటి type మరియు equality result ను console లో print చేయండి.
Hint:
రెండు Symbols కోసం Symbol(“user-token”) ను రెండుసార్లు ఉపయోగించండి. Equality check కోసం === operator ఉపయోగించండి.
Common Mistakes
null మరియు undefined రెండూ ఒకటే అని అనుకోవడం.
undefined అంటే value assign చేయలేదు లేదా value అందుబాటులో లేదు. null అంటే value లేదని developer ఉద్దేశపూర్వకంగా సూచించాడు.
ఒకే description ఉన్న రెండు Symbols equal అవుతాయని అనుకోవడం.
ప్రతి Symbol unique. Symbol(“id”) === Symbol(“id”) ఫలితం false అవుతుంది.
typeof null ఫలితం null వస్తుందని అనుకోవడం.
JavaScript historical behavior వల్ల typeof null ఫలితం “object” వస్తుంది. null నిజంగా object కాదు.
Symbol description ను Symbol యొక్క actual identity గా భావించడం.
Symbol లోని description కేవలం మనకు గుర్తించడానికి ఉపయోగపడుతుంది. Uniqueness ప్రతి Symbol creation తో వస్తుంది.
Interview Questions
JavaScript లో null అంటే ఏమిటి?
Answer:
null అనేది intentional absence of value ను సూచించే primitive value. Developer ఒక variable లో ప్రస్తుతం value లేదని స్పష్టంగా చెప్పడానికి దీనిని ఉపయోగిస్తాడు.
null మరియు undefined మధ్య తేడా ఏమిటి?
Answer:
undefined సాధారణంగా value assign చేయనప్పుడు కనిపిస్తుంది. null మాత్రం value లేదని developer ఉద్దేశపూర్వకంగా assign చేసిన value.
typeof null ఫలితం ఏమిటి? ఎందుకు?
Answer:
typeof null ఫలితం “object”. ఇది JavaScript లోని పాత historical behavior. null నిజంగా object కాదు.
Symbol ఎందుకు ఉపయోగిస్తారు?
Answer:
Symbol unique identifiers లేదా object properties కు unique keys సృష్టించడానికి ఉపయోగిస్తారు. ఒకే description ఉన్నా ప్రతి Symbol వేర్వేరు value అవుతుంది.
Exam Questions
క్రింది code యొక్క output ఏమిటి? const a = Symbol(“x”); const b = Symbol(“x”); console.log(a === b);
Answer:
Output false. రెండు Symbols కు ఒకే description ఉన్నా, అవి unique values కాబట్టి equal కావు.
typeof null expression యొక్క output ఏమిటి?
Answer:
Output “object”. ఇది JavaScript యొక్క historical behavior.
ఒక variable లో value intentionally లేదు అని సూచించడానికి ఏ value ఉపయోగిస్తారు?
Answer:
null ను ఉపయోగిస్తారు.
Quick Revision
- null intentional absence of value ను సూచిస్తుంది.
- undefined మరియు null వేర్వేరు values.
- typeof null ఫలితం “object”, ఇది historical behavior.
- Symbol() ప్రతి సారి unique primitive value ను సృష్టిస్తుంది.
- ఒకే description ఉన్న రెండు Symbols కూడా strict equality లో equal కావు.
- Symbol ను unique object keys లేదా identifiers కోసం ఉపయోగించవచ్చు.
