JavaScript Me Data Types Kya Hote Hain – Data Types in JavaScript

JavaScript Me Data Types Kya Hote Hain?

JavaScript ek dynamically typed language hai, jisme variables kisi bhi type ka data store kar sakte hain. JavaScript ke data types do categories mein divide hote hain:

  • Primitive Types
  • Reference Types

Chaliye inko detail mein samajhte hain.

1. Primitive Data Types :

Primitive data types woh basic data types hote hain jo directly stack memory me store hote hain aur immutable (change nahi hote) hote hain. Ye values directly assign hote hain aur kisi reference ka use nahi karte. JavaScript me primitive types me Number, String, Boolean, Null, Undefined, Symbol, aur BigInt shamil hote hain. Jab ek primitive value kisi naye variable me assign ki jati hai, to uski ek nayi copy create hoti hai, na ki reference. Is wajah se, ek variable me change dusre variable ko affect nahi karta. Ye simple aur fast execution ke liye design kiye gaye hote hain.

(i) Number :

JavaScript me Number ek primitive data type hai jo integer, floating-point, Infinity, aur NaN (Not-a-Number) values ko represent karta hai. Yeh arithmetic operations support karta hai aur iska range -(2^53 - 1) se (2^53 - 1) tak hota hai. Numbers automatic type conversion support karte hain.

let age = 25;
let price = 99.99;

(ii) String :

String ek sequence hota hai characters ka jo text represent karta hai. JavaScript me, strings immutable hote hain aur single ('), double (") ya template literals (`) me likhe jate hain. Ye ek primitive data type hai jo indexing ke madhyam se access aur manipulate kiya jata hai.

let name = "Rahul";
let greeting = 'Hello, World!';
let message = `Welcome, ${name}!`;

(iii) Boolean :

Boolean ek data type hai jo sirf do values store kar sakta hai: true ya false. Yeh logical operations aur decision-making me use hota hai. JavaScript, Python jaise programming languages me Boolean values conditions evaluate karne aur comparisons perform karne ke liye istemal hoti hain, jisse program execution control hota hai.

let isStudent = true;
let hasLicense = false;

(iv) Null :

“Null” ek primitive data type hai jo intentional non-value ko represent karta hai. Jab kisi variable ki value null hoti hai, iska matlab hai ki usme koi object ya value assigned nahi hai. Yeh intentionally empty hota hai aur JavaScript me ek special keyword ke roop me aata hai.

let emptyValue = null;

(v) Undefined :

Undefined ek primitive data type hai jo tab hota hai jab kisi variable ko declare kiya jata hai par usko koi value assign nahi ki jati. JavaScript me agar koi function explicitly return nahi karta, ya koi non-existent property access ki jaye, to bhi undefined return hota hai.

let notAssigned;
console.log(notAssigned); // Output: undefined

(vi) Symbol (ES6 mein introduce hua) :

Symbol ek unique aur immutable primitive data type hai jo JavaScript me ek unique identifier create karne ke liye use hota hai. Har Symbol value ek naya aur unique reference hota hai, jo kisi bhi existing value se match nahi hota, isliye ye objects me unique property keys ke liye useful hai.

let sym1 = Symbol("uniqueValue");
let sym2 = Symbol("uniqueValue");
console.log(sym1 === sym2); // Output: false

(vii) BigInt (ES11 mein introduce hua) :

BigInt ek JavaScript data type hai jo bohot bade integers ko store karne ke liye use hota hai, jo Number type se bahar hote hain. Iska use (2⁵³ – 1 se bade) handle karne ke liye hota hai. BigInt ko n suffix se represent kiya jata hai.

let bigNumber = 9007199254740991n;
console.log(bigNumber + 1n); // Output: 9007199254740992n

2. Reference Data Types (Non-Primitive Types)

Reference data types complex data structures hote hain jo actual values ko directly store nahi karte, balki unka memory address rakhte hain. Jab kisi reference data type ko ek variable me assign kiya jata hai, to woh variable value ko directly store nahi karta, balki us memory location ka reference hold karta hai jahan actual data heap memory me store hota hai. Primitive data types ke opposite, jo stack memory me directly value store karte hain, reference data types multiple variables ko same data access aur modify karne ki permission dete hain, jisse dynamic aur flexible data handling possible hoti hai.

(i) Object :

Object ek JavaScript data type hai jo key-value pairs store karta hai. Yeh ek reference type hota hai, jisme properties aur methods hoti hain. Objects heap memory me store hote hain aur inka reference variable ke through access hota hai. Yeh complex data structures ko represent karne ke liye use hote hain.

let person = {
    name: "Amit",
    age: 30,
    isMarried: false
};
console.log(person.name); // Output: Amit

(ii) Array :

Array ek ordered collection hota hai jo multiple values ko ek single variable me store karne ki suvidha deta hai. Yeh index-based hota hai, jisme har element ek unique position par store hota hai. JavaScript me arrays dynamic hote hain, jisme alag-alag data types ek saath store kiye ja sakte hain.

let fruits = ["Apple", "Mango", "Banana"];
console.log(fruits[0]); // Output: Apple

(iii) Function :

Function ek reusable block of code hota hai jo specific task perform karta hai. Isko define karne ke baad multiple times call kiya ja sakta hai. Functions input (parameters) le sakte hain aur output (return value) de sakte hain, jisse code modular, efficient aur maintainable banta hai.

function greet(name) {
    return `Hello, ${name}!`;
}
console.log(greet("Priya")); // Output: Hello, Priya!

Primitive vs Reference Types ka Difference

Feature Primitive Types Reference Types
Storage Value directly store hoti hai Memory location ka reference store hota hai
Mutability Immutable (change nahi ho sakta) Mutable (change ho sakta hai)
Example Number, String, Boolean Object, Array, Function

 

JavaScript mein Primitive aur Reference types ka concept samajhna bohot important hai. Primitive types simple aur immutable hote hain, jabki reference types complex hote hain aur inka reference memory mein store hota hai. Yeh concepts JavaScript development ke liye fundamental hote hain aur inko sahi tarike se use karne se performance aur functionality improve hoti hai.

Quiz: Test Your Knowledge on Data Types in JavaScript

Bonus: Practical Application!

Aaj hi JavaScript ke different data types ka istemal karke coding practice karein!

JavaScript data types ko sahi tareeke se samajhne ke liye different types jaise String, Number, Boolean, Undefined, Null, Object, Symbol, aur BigInt ka upayog karein aur apne JavaScript skills ko aur bhi improve karein.

Leave a Reply