JavaScript BigInt and typeof
JavaScript BigInt మరియు typeof
JavaScript లో చాలా పెద్ద whole numbers ను handle చేయడానికి BigInt data type ఉపయోగిస్తారు. సాధారణ Number data type లో safe integer limit ఉంటుంది. ఆ పరిమితిని దాటినప్పుడు ఖచ్చితమైన integer calculations కోసం BigInt ఉపయోగించాలి.
BigInt ఎలా రాయాలి?
ఒక integer value చివర n రాస్తే అది BigInt అవుతుంది. ఉదాహరణ: 9007199254740993n. ఇక్కడ n suffix చాలా ముఖ్యమైనది.
BigInt operations
రెండు BigInt values ను కలపడానికి రెండింటినీ BigInt గానే ఉంచాలి. ఉదాహరణకు 10n + 5n సరైనది. కానీ 10n + 5 చేయడం వల్ల TypeError వస్తుంది, ఎందుకంటే BigInt మరియు Number ను సాధారణ arithmetic operation లో కలపలేము.
BigInt division లో decimal part తొలగించబడుతుంది. ఉదాహరణకు 7n / 2n ఫలితం 3n అవుతుంది.
typeof operator
typeof operator ఒక value యొక్క data type ను string రూపంలో చెబుతుంది. ఉదాహరణకు typeof 25 ఫలితం “number”, మరియు typeof 25n ఫలితం “bigint”.
గమనించవలసిన ఒక ప్రత్యేక విషయం: typeof null ఫలితం “object”. ఇది JavaScript లో ఉన్న పాత language behavior. అలాగే arrays మరియు objects రెండింటికీ సాధారణంగా “object” వస్తుంది.
Simple Explanation
BigInt అంటే చాలా పెద్ద integer numbers కోసం ఉపయోగించే data type. Number value లాగా decimal point ఉండదు మరియు value చివర n ఉంటుంది.
typeof అంటే ఒక value ఏ data type కు చెందినదో తెలుసుకునే operator. ఉదాహరణకు typeof 100n మనకు “bigint” ఇస్తుంది.
- 25 → number
- 25n → bigint
- “Hello” → string
- true → boolean
- null → object
BigInt values తో calculations చేస్తున్నప్పుడు అన్ని operands BigInt గా ఉండేలా చూసుకోండి.
Real-Life Example
ఒక దేశంలోని మొత్తం population, ప్రపంచవ్యాప్తంగా website visits, లేదా bank transaction IDs చాలా పెద్ద సంఖ్యలకు చేరవచ్చు. అలాంటి integer values ను decimal precision కోల్పోకుండా ఉంచడానికి BigInt ఉపయోగించవచ్చు.
ఉదాహరణకు, ఒక website కు 9007199254740993 visits ఉంటే, ఆ సంఖ్యను BigInt గా store చేసి మరిన్ని visits ను ఖచ్చితంగా add చేయవచ్చు. Value చివర n ఉపయోగించడం మర్చిపోవద్దు.
Code Examples
BigInt value మరియు arithmetic
const population = 9007199254740993n;
const additionalPeople = 7n;
console.log(typeof population);
console.log(population + additionalPeople);
ఇక్కడ population మరియు additionalPeople రెండూ BigInt values. typeof population దాని type ను చూపిస్తుంది. రెండు BigInt values ను add చేయడం వల్ల పెద్ద integer ఖచ్చితంగా calculate అవుతుంది.
bigint 9007199254741000n
వివిధ values తో typeof ఉపయోగించడం
const age = 25;
const price = 99.5;
const userId = 123n;
const userName = "Anu";
const isActive = true;
const emptyValue = null;
console.log(typeof age);
console.log(typeof price);
console.log(typeof userId);
console.log(typeof userName);
console.log(typeof isActive);
console.log(typeof emptyValue);
ఈ code లో వివిధ data types ను typeof తో పరీక్షిస్తున్నాం. Integer మరియు decimal values కు number వస్తుంది. 123n కు bigint వస్తుంది. null కు JavaScript behavior కారణంగా object వస్తుంది.
number number bigint string boolean object
BigInt division మరియు comparison
const totalItems = 7n;
const boxes = 2n;
console.log(totalItems / boxes);
console.log(totalItems > boxes);
console.log(10n === 10n);
BigInt division లో decimal part ఉంచబడదు, అందుకే 7n / 2n ఫలితం 3n. BigInt values ను relational operators తో compare చేయవచ్చు. రెండు ఒకే BigInt values ను strict equality తో compare చేస్తే true వస్తుంది.
3n true true
Line-by-Line Explanation
ఈ line లో population అనే constant variable ను create చేసి, చివర n ఉన్నందువల్ల దానికి BigInt value assign చేస్తున్నాం.
ఇక్కడ additionalPeople variable లో 7 అనే BigInt value ను store చేస్తున్నాం. Arithmetic సమయంలో ఇది Number కాదు, BigInt.
ఈ ఖాళీ line code ను చదవడానికి సులభంగా ఉండేందుకు variables మరియు output statements మధ్య spacing ఇస్తుంది. Program execution పై దీని ప్రభావం ఉండదు.
typeof population ద్వారా population యొక్క data type ను తెలుసుకుంటున్నాం. ఎందుకంటే అది BigInt కాబట్టి console లో bigint కనిపిస్తుంది.
ఇక్కడ రెండు BigInt values ను add చేస్తున్నాం. రెండూ ఒకే type లో ఉండటం వల్ల addition సరిగ్గా execute అవుతుంది మరియు పెద్ద integer result వస్తుంది.
Try It
Try this JavaScript code:
const distance = 9007199254740993n;
const extraDistance = 7n;
console.log("Type:", typeof distance);
console.log("Total:", distance + extraDistance);
Practice
Practice:
websiteVisits యొక్క data type ను typeof ఉపయోగించి print చేయండి. తరువాత websiteVisits మరియు newVisits ను add చేసి మొత్తం visits ను print చేయండి.
Hint:
రెండు values చివర n ఉన్నందున అవి BigInt. Type కోసం typeof websiteVisits ఉపయోగించండి. Addition లో Number value ను కలపకండి.
Common Mistakes
చాలా పెద్ద number ను 9007199254740993 లాగా n లేకుండా రాయడం.
ఇది BigInt కావాలంటే value చివర n రాయాలి: 9007199254740993n.
10n + 5 లాగా BigInt మరియు Number ను ఒకే arithmetic operation లో కలపడం.
రెండు operands ను ఒకే type లో ఉంచండి. ఉదాహరణకు 10n + 5n ఉపయోగించండి.
typeof null ఫలితం “null” వస్తుందని అనుకోవడం.
JavaScript లో typeof null ఫలితం “object”. ఇది language యొక్క historical behavior.
BigInt division decimal result ఇస్తుందని అనుకోవడం.
BigInt division లో decimal part తొలగించబడుతుంది. ఉదాహరణకు 7n / 2n ఫలితం 3n.
Interview Questions
JavaScript లో BigInt అంటే ఏమిటి?
Answer:
BigInt అనేది safe integer range కంటే పెద్ద whole numbers ను ఖచ్చితంగా represent చేయడానికి ఉపయోగించే primitive data type. Literal ను రాయేటప్పుడు సాధారణంగా integer చివర n suffix ఉపయోగిస్తారు.
typeof 100n ఫలితం ఏమిటి?
Answer:
దాని ఫలితం “bigint”. BigInt కోసం typeof ప్రత్యేకంగా “bigint” string ను return చేస్తుంది.
BigInt మరియు Number ను direct arithmetic లో ఎందుకు కలపకూడదు?
Answer:
BigInt మరియు Number వేర్వేరు numeric types. Precision మరియు type consistency సమస్యలను నివారించడానికి JavaScript వీటిని direct arithmetic operation లో కలపడానికి అనుమతించదు. అవసరమైతే operands ను సరైన type కు convert చేయాలి.
typeof null ఫలితం ఏమిటి?
Answer:
typeof null ఫలితం “object”. ఇది JavaScript లోని పాత historical behavior; null నిజంగా object కాదు.
Exam Questions
క్రింది code output ఏమిటి? console.log(typeof 500n);
Answer:
Output: “bigint”
క్రింది expression లో సమస్య ఏమిటి? 20n + 10
Answer:
ఇది BigInt మరియు Number ను కలుపుతోంది. అందువల్ల TypeError వస్తుంది. సరైన రూపం 20n + 10n లేదా అవసరాన్ని బట్టి రెండు values ను Number గా ఉంచడం.
7n / 2n ఫలితం ఏమిటి?
Answer:
ఫలితం 3n. BigInt division decimal part ను ఉంచదు.
BigInt value ను గుర్తించే syntax ఏమిటి?
Answer:
Integer literal చివర చిన్న అక్షరం n ఉంచాలి. ఉదాహరణ: 12345678901234567890n.
Quick Revision
- BigInt చాలా పెద్ద whole numbers కోసం ఉపయోగించే JavaScript data type.
- BigInt literal చివర n suffix ఉంటుంది, ఉదాహరణకు 100n.
- BigInt మరియు Number ను direct arithmetic operation లో కలపకండి.
- typeof ఒక value యొక్క data type ను string రూపంలో return చేస్తుంది.
- typeof 25n ఫలితం “bigint”.
- typeof null ఫలితం “object”.
- BigInt division లో decimal part తొలగించబడుతుంది.
