JS Data Types
In this page:
let text = "string";
let number = 10;
let flag = true;
let list = [item1, item2];
let object = { key: value };
String, Number और Boolean
string उद्धरण चिह्नों में लिपटे text को दर्शाता है, number पूर्ण और दशमलव दोनों संख्याओं को एक ही type से दर्शाता है, और boolean केवल true या false दर्शाता है, रोज़मर्रा की JavaScript में सबसे आम तौर पर इस्तेमाल होने वाले तीन data types।
उदाहरण: String, Number, and Boolean
// Declare the variable `name`, set to "Alice"
// Declare the variable `name`, set to "Alice"
let name = "Alice";
// Declare the variable `age`, set to `30`
// Declare the variable `age`, set to `30`
let age = 30;
// Declare the variable `isActive`, set to `true`
// Declare the variable `isActive`, set to `true`
let isActive = true;
// Print `typeof name, typeof age, typeof isActive` to the console
// Print `typeof name, typeof age, typeof isActive` to the console
console.log(typeof name, typeof age, typeof isActive);
undefined और null
undefined का मतलब है कि variable घोषित हुआ है पर उसे कभी value नहीं दिया गया, जबकि null एक इरादतन value है जो developers 'कुछ नहीं' या 'जानबूझकर खाली' दर्शाने के लिए देते हैं, दोनों असली value की अनुपस्थिति का अर्थ रखने के बावजूद अलग अवधारणाओं को दर्शाते हैं।
उदाहरण: undefined and null
let notSet;
let empty = null;
console.log(notSet); // undefined
console.log(empty); // null
bigint और symbol
bigint उन पूर्ण संख्याओं को दर्शाता है जो सामान्य number type सुरक्षित रूप से नहीं संभाल सकता, जो n suffix के साथ लिखी जाती हैं, जबकि symbol एक गारंटीशुदा अद्वितीय value बनाता है, जो अक्सर विशेष, टकराव-मुक्त property key के रूप में इस्तेमाल होता है।
उदाहरण: bigint and symbol
// Declare the variable `big`, set to `12345678901234567890n`
// Declare the variable `big`, set to `12345678901234567890n`
let big = 12345678901234567890n;
// Declare the variable `id`, set to `Symbol("id")`
// Declare the variable `id`, set to `Symbol("id")`
let id = Symbol("id");
// Print `typeof big, typeof id` to the console
// Print `typeof big, typeof id` to the console
console.log(typeof big, typeof id);
object Type
object इकलौता non-primitive type है, जो संबंधित data और functionality को एक साथ समूहित करने के लिए इस्तेमाल होता है, और इसमें केवल साधारण objects ही नहीं बल्कि arrays, functions और dates भी आते हैं, जो सभी typeof के साथ object या function बताते हैं।
उदाहरण: The object Type
// Declare the variable `user`, set to `{ name: "Sam" }`
// Declare the variable `user`, set to `{ name: "Sam" }`
let user = { name: "Sam" };
// Declare the variable `list`, set to `[1, 2, 3]`
// Declare the variable `list`, set to `[1, 2, 3]`
let list = [1, 2, 3];
// Print `typeof user, typeof list` to the console
// Print `typeof user, typeof list` to the console
console.log(typeof user, typeof list);
Dynamic Typing
JavaScript dynamically typed है, यानी declaration के समय variable का type तय नहीं होता, वह इस बात से तय होता है कि उसके अंदर अभी कौन सा value बैठा है, और वह value, अपने type के साथ, बदल सकता है अगर variable को दोबारा assign किया जाए।
उदाहरण: Dynamic Typing
let value = 5;
console.log(typeof value); // "number"
value = "now a string";
console.log(typeof value); // "string"
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: