Skip to content
This page has been auto-translated and may contain errors.View in English

डेटा types

docs.scrimba.com

किसी के नाम, उनकी उम्र, और क्या वे logged in हैं, को store करें, और आप पहले से ही तीन अलग-अलग तरह की values का उपयोग कर रहे हैं। नाम text है, उम्र एक number है, और logged-in flag एक हां-या-नहीं है। JavaScript इनमें से प्रत्येक को एक अलग type के रूप में मानता है, और यह प्रत्येक को अलग तरीके से संभालता है। यह अध्याय उन कुछ types के बारे में है जो आप सबसे पहले मिलते हैं, और यह पहचानने के बारे में है कि आप किस one को धारण कर रहे हैं।

मूल types

तीन types ज्यादातर चीजों को कवर करते हैं जो आप store करते हैं। एक string text है, जो quotes में wrapped है। एक number कोई भी संख्या है, गिनती या मापने के लिए। एक boolean एक हां-या-नहीं value है, और इसके केवल दो विकल्प हैं: true या false

js
const name = "प्रिया"; // एक string: text
const age = 27; // एक number
const isLoggedIn = true; // एक boolean: true या false

आप किसी भी चीज़ के लिए एक string का उपयोग करते हैं जो शब्दों जैसी दिखती है: एक नाम, एक संदेश, एक रंग। आप किसी भी चीज़ के लिए एक number का उपयोग करते हैं जिसे आप गिन या calculate कर सकते हैं। आप एक boolean का उपयोग करते हैं जब भी answer दो विकल्पों में से एक होता है, जैसे कि क्या कोई box checked है। आप इन्हें strings और numbers में बहुत अधिक देखेंगे।

JavaScript की everyday values कुछ types में fall करती हैं, और उनमें से तीन आपके अधिकांश data को carry करती हैं। एक string quotes में text है, single या double, इससे कोई फर्क नहीं पड़ता कि कौन सा है। एक number कोई भी numeric value है। एक boolean बिल्कुल दो values में से एक है, true या false

js
const productName = "वायरलेस माउस"; // string
const price = 24.99; // number
const inStock = true; // boolean

प्रत्येक type का एक काम है। Strings text को hold करती हैं जो आप दिखाते या पढ़ते हैं, जो strings में covered है। Numbers कुछ भी hold करती हैं जो आप calculate करते हैं, जो numbers में covered है। Booleans एक two-way answer hold करती हैं और जहाँ कहीं भी कोई decision बनाया जाता है, क्योंकि एक comparison जैसे price < 30 एक produce करता है।

JavaScript values को एक small set of types में sort करता है, और उनमें से तीन आपके अधिकांश data को account करती हैं। एक string quotes में लिखे characters का एक sequence है, single, double, या backticks template literals के लिए। एक number एक numeric value है। एक boolean दो values में से एक है, true या false

js
const currency = "EUR"; // string
const balance = 1499.5; // number
const isOverdrawn = balance < 0; // boolean, यहाँ एक comparison का result

Strings और numbers के बीच का distinction पहली नज़र की तुलना में अधिक महत्वपूर्ण है, क्योंकि "5" और 5 अलग तरीके से behave करते हैं: एक text है जिसमें एक digit होता है, दूसरा एक value है जिसके साथ आप arithmetic कर सकते हैं। Booleans जल्दी notice करने लायक हैं क्योंकि आप उन्हें rarely manually type करते हैं। वे comparisons और logical checks से बाहर आते हैं, इसलिए balance < 0 को "एक boolean" के रूप में पढ़ने की ability ही है जो बाद के conditions और operators को place में fall करता है।

Junoमूल types तीन जो आप constantly reach करते हैं: string quotes में text के लिए, number किसी भी numeric के लिए, और boolean एक true या false answer के लिए। उस type को pick करें जो आप store कर रहे हैं चीज़ की तरह match करता है। एक name एक string है, एक age एक number है, एक checkbox एक boolean है।
Junoमूल types तीन types ज्यादातर काम करती हैं: string text के लिए, number numeric values के लिए, boolean एक true या false के लिए। Quotes एक string बनाते हैं, इसलिए "5" text है, एक number नहीं। Booleans आमतौर पर एक comparison के result के रूप में आते हैं कुछ के बजाय जो आप type करते हैं।
Junoमूल types Strings, numbers, और booleans आपके अधिकांश data को carry करती हैं, और string-versus-number line वह है जो bite करती है: "5" text है, 5 arithmetic है। आप almost never एक boolean literal लिखते हैं, क्योंकि comparisons जैसे balance < 0 आपको एक hand करते हैं। एक comparison को "यह एक boolean है" के रूप में पढ़ना ही है जो बाद के conditions को click into place करता है।

कुछ नहीं: null और undefined

कभी-कभी एक value missing होता है, और JavaScript के पास इसे कहने के लिए दो तरीके हैं। undefined का मतलब है कि अभी तक कोई value दी नहीं गई है। अगर आप एक variable बनाते हैं बिना इसे set किए, इसकी value undefined है।

js
let score;
console.log(score); // undefined

null का मतलब deliberately empty है। आप इसे स्वयं use करते हैं, कहने के लिए "इसके पास अभी कोई value नहीं है, और मैं इसका मतलब रखता था"।

js
let winner = null;
console.log(winner); // null

Short version: undefined है JavaScript कहना "यहाँ अभी तक कुछ नहीं", और null है आप कहना "यहाँ कुछ नहीं, deliberately"।

दोनों values "no value" के लिए खड़ी हैं, और difference यह है कि किसने उन्हें set किया। undefined वह है जो JavaScript आपको by default देता है: एक variable declared बिना एक value के, उदाहरण के लिए, शुरू होता है undefined के रूप में।

js
let selectedColor;
console.log(selectedColor); // undefined

null एक value है जो आप स्वयं assign करते हैं कुछ को deliberately empty mark करने के लिए, एक slot जिसे आपने clear किया है या अभी तक fill नहीं किया है।

js
let couponCode = null;
console.log(couponCode); // null

undefined default absence है; null deliberately one है। यह split उन्हें read करने का useful तरीका है: अगर आप undefined देखते हैं, कुछ कभी set नहीं हुआ था; अगर आप null देखते हैं, किसी ने इसे empty करना choose किया।

null और undefined दोनों absence का प्रतिनिधित्व करते हैं, और convention जो उन्हें separate करती है वह intent के बारे में है। undefined वह absence है जो JavaScript स्वयं produce करता है: एक declared-but-unassigned variable, एक missing property, एक function जो कुछ नहीं return करता।

js
let discount;
console.log(discount); // undefined

null वह absence है जो आप deliberately assign करते हैं, signal करने के लिए "intentionally empty" कि "not yet touched" के बजाय।

js
let appliedCoupon = null;
console.log(appliedCoupon); // null

दोनों को अलग रखना एक real signal है एक codebase में: undefined का मतलब आमतौर पर है कि किसी ने यह set नहीं किया है, जो एक bug को point कर सकता है या एक unfinished path, जबकि null एक considered choice के रूप में reads करता है। वे बाद के checks के under identically भी behave नहीं करते हैं, यही कारण है कि उन्हें blur करने से confusion होती है एक बार जब comparisons और operators picture में enter करते हैं। undefined को machine's default मानें और null को अपने explicit "empty" के रूप में मानें, और उनका behaviour random होना बंद कर देता है।

Junoकुछ नहीं: null और undefined "no value" कहने के दो तरीके। undefined है JavaScript का default जब कुछ भी set नहीं हुआ है। null वह है जो आप अपने आप लिखते हैं "empty on purpose" का मतलब। अगर आपने कभी एक variable को value नहीं दी, तो undefined expect करें।
Junoकुछ नहीं: null और undefined दोनों का मतलब है absence, difference यह है कि किसने इसे set किया। undefined अपने आप show होता है, जैसे एक variable declared बिना value के। null वह है जो आप assign करते हैं कुछ को deliberately empty mark करने के लिए। उन्हें उस तरीके से read करने से आपको बताता है कि एक absence एक accident था या एक choice।
Junoकुछ नहीं: null और undefined Convention यहाँ heavy lifting करता है: undefined वह है absence जो JavaScript आपको hand करता है, null वह है absence जो आप deliberately assign करते हैं। तो undefined अक्सर flag करता है "किसी ने यह set नहीं किया", जो एक bug हो सकता है, जबकि null intent के रूप में reads करता है। उन्हें distinct रखें और बाद के comparisons आपको surprise करना बंद कर देते हैं।

typeof के साथ एक type को check करना

जब आप निश्चित नहीं हैं कि एक value किस type की है, typeof आपको बताता है। इसे एक value के सामने रखें और यह एक string return करता है जो type को name करती है।

js
console.log(typeof "प्रिया"); // "string"
console.log(typeof 27); // "number"
console.log(typeof true); // "boolean"

ध्यान दें कि answer हमेशा text है, एक string जैसे "number", type स्वयं नहीं। यह handy है जब आप check करना चाहते हैं कि आप किससे deal कर रहे हैं इसे use करने से पहले।

typeof एक value के type को एक string के रूप में report करता है। किसी भी value के सामने रखें और आपको "string", "number", "boolean", आदि वापस मिलता है।

js
const label = "checkout";
console.log(typeof label); // "string"
console.log(typeof 24.99); // "number"
console.log(typeof false); // "boolean"
console.log(typeof undefined); // "undefined"

एक quirk है जो जानने लायक है अभी, क्योंकि यह पहली बार सभी को surprise करता है: typeof null है "object", "null" नहीं।

js
console.log(typeof null); // "object"

यह language में एक long-standing bug है जो कभी fix नहीं हुई, क्योंकि इसे fix करने से old code break हो जाएगा। यह कुछ नहीं है जो आपने गलत किया। याद रखें कि typeof null lies करता है, और null को directly check करें जब आपको की जरूरत हो।

typeof एक value लेता है और एक string return करता है जो इसकी type को name करती है। यह सबसे तेज़ तरीका है यह पूछने का "मैं क्या hold कर रहा हूँ" runtime में।

js
console.log(typeof "checkout"); // "string"
console.log(typeof 1499.5); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"

एक result जिसे आपको memorise करना चाहिए वह है outlier: typeof null return करता है "object", "null" नहीं।

js
console.log(typeof null); // "object"

यह एक historical quirk है, एक bug जो JavaScript के first version में baked है जो कभी corrected नहीं हुई क्योंकि बहुत सारे existing code अब wrong answer पर depend करता है। Practical consequence यह है कि typeof alone आपको यह नहीं बता सकता कि एक value null है; आप इसे एक direct comparison के साथ check करते हैं। यह है JavaScript के backward-compatibility trade-off का classic example, जहाँ एक known mistake अपनी जगह पर रहता है क्योंकि इसे break करने से web break हो जाएगा।

Junotypeof के साथ एक type को check करना एक value के सामने typeof रखें और यह आपको type बताता है, एक string के रूप में जैसे "number"। यह सबसे तेज़ तरीका है check करने का कि आप क्या hold कर रहे हैं जब आप unsure हैं। Answer हमेशा text है, तो typeof 27 आपको "number" देता है, quotes और सभी के साथ।
Junotypeof के साथ एक type को check करनाtypeof आपको type को एक string के रूप में hand करता है, जो आपको एक value को check करने देता है इसे use करने से पहले। एक trap को देखें: typeof null है "object", "null" नहीं। यह एक bug है जिसे language ने compatibility के लिए रखा, तो null को directly test करें कि typeof को trust करने के बजाय।
Junotypeof के साथ एक type को check करनाtypeof answers करता है "यह किस type का है" एक string के रूप में runtime में। एक result जिसे memorise करना है वह है wrong one: typeof null है "object", एक first-version bug जो place में frozen है क्योंकि इसे fix करने से web break हो जाएगा। तो यह कभी भी null को आपके लिए confirm नहीं कर सकता; एक direct comparison के साथ उसे check करें।

Types का बड़ा चित्र

आप अब तक मिली everyday types को मिले हो: string, number, boolean, plus null और undefined। यह काफी है store करने के लिए अधिकांश जो एक छोटा program चाहिए। बाद के अध्याय values को एक साथ group करने के तरीके जोड़ते हैं, लेकिन उनमें से प्रत्येक still built है simple types से जो आप पहले से ही देख चुके हैं।

अभी के लिए, win यह जानना है कि हर value के पास एक type है, और type decide करता है कि आप value के साथ क्या कर सकते हैं। एक number आप add कर सकते हैं; एक string आप join कर सकते हैं; एक boolean आप check कर सकते हैं। operators को next step के रूप में याद रखें, क्योंकि यही है जहाँ आप इन values को combine करना शुरू करते हैं।

अब तक के types, string, number, boolean, null, और undefined, हैं primitive types: simple, single values। JavaScript के पास non-primitive types भी हैं, जो values के collections को hold करते हैं, जैसे arrays और objects। उन्हें अपने chapters में cover किया जाता है, और वे उन तरीकों में differently behave करते हैं जो learn करने लायक हैं जब आप वहाँ जाते हैं।

एक चीज़ को अभी clear करने के लिए: number एक single type है। JavaScript whole numbers और decimals को separate types में split नहीं करता जिस तरीके से कुछ languages करते हैं, तो 10 और 10.5 दोनों same number type हैं। यह numbers को simpler रखता है reason about करने के लिए, एक type के साथ हर numeric value को covering करते हैं।

यहाँ cover किए गए types, string, number, boolean, null, और undefined, हैं primitive types: single, self-contained values। बाकी सब कुछ, arrays और objects, एक reference type है, जो एक collection को hold करता है और अलग तरीके से behave करता है जब आप इसे copy या compare करते हैं। वह difference बहुत सारे surprises का source है, और यह अपना treatment लेता है जब वह types arrive करते हैं।

कुछ चीज़ें flag करने लायक हैं जबकि types table पर हैं। number हर numeric value के लिए एक type है, whole या decimal, तो कोई separate integer type नहीं है reason about करने के लिए, numbers में covered। दो further primitives exist करते हैं जो आप early में rarely मिलते हैं, bigint integers के लिए बहुत large एक normal number के लिए, और symbol unique keys के लिए। और types fixed नहीं हैं place में: JavaScript एक को दूसरे में convert करेगा कुछ situations में, एक behaviour जिसे coercion कहा जाता है (उदाहरण के लिए एक number को एक string में turn करना जब आप उन्हें join करते हैं)। इसके real consequences हैं, और operators यही है जहाँ यह matter करना शुरू करता है।

JunoTypes का बड़ा चित्र हर value के पास एक type है, और type decide करता है कि आप इसके साथ क्या कर सकते हैं: numbers add करें, strings join करें, booleans check करें। Everyday types, string, number, boolean, plus null और undefined, अधिकांश को cover करते हैं जो एक छोटा program stores करता है। बाद के अध्याय values को एक साथ group करते हैं, लेकिन वे सभी इनसे built हैं।
JunoTypes का बड़ा चित्र पाँच जो आप जानते हैं, string, number, boolean, null, और undefined, हैं primitive types: simple single values। Arrays और objects हैं non-primitives, बाद में आने के लिए, और वे differently behave करते हैं। Handy fact: number है 10 और 10.5 दोनों के लिए एक type, तो कोई separate integer और decimal नहीं track करने के लिए।
JunoTypes का बड़ा चित्र Primitives single values हैं; arrays और objects reference types हैं जो अलग तरीके से copy और compare करते हैं, जो है जहाँ surprises live करते हैं। number हर numeric value को cover करता है, और bigint और symbol exist करते हैं rare cases के लिए। Types भी एक दूसरे में convert करते हैं, जिसे coercion कहा जाता है, और operators यही है जहाँ यह matter करना शुरू करता है, तो इसे अपने mind के back में रखें।

Types यहाँ से कहाँ जाते हैं

हर value जो आप लिखते हैं, हर एक जिसे आप वापस पढ़ते हैं, एक type carry करता है, और इसे name करने में fluent होना base है सब कुछ के लिए जो follow करता है। अगले अध्याय दोनों को लेते हैं जिन्हें आप सबसे अधिक handle करेंगे और deep जाते हैं: strings text के साथ काम करने के लिए, और numbers calculation के लिए। उसके बाद, operators यही है जहाँ ये types एक दूसरे को meet करना शुरू करते हैं, comparing, combining, और कभी-कभी converting, और knowing कि आप किस type को hold करते हैं यही है जो इसे आपको surprise होने से रखता है।