|4. Data Types — Primitives आणि Objects
Chapter 4JavaScript Tutorial~1 min read

Data Types — Primitives आणि Objects

JavaScript चे 7 Primitive Types

JavaScript मधील प्रत्येक value एका data type चा असतो. Data types दोन categories मध्ये divide होतात: Primitive Types (simple values) आणि Object Types (complex values).

7 Primitive Data Types

Primitive Types — उदाहरण

javascript
// 1. Number — Integer आणि Decimal दोन्ही
let age = 25;
let price = 999.99;

// 2. String — Text, quotes मध्ये
let name = "Rahul";
let city = 'Pune';

// 3. Boolean — true किंवा false
let isLoggedIn = true;
let hasAccount = false;

// 4. Null — Intentionally empty (programmer set करतो)
let selectedItem = null;

// 5. Undefined — Declared पण value नाही
let result;
console.log(result); // undefined

// 6. Symbol — Unique identifier (ES6)
let id = Symbol("userId");

// 7. BigInt — खूप मोठे numbers (ES2020)
let bigNumber = 9007199254740991n;

typeof — Type Check करणे

typeof Operator

javascript
console.log(typeof 42);          // "number"
console.log(typeof "Hello");     // "string"
console.log(typeof true);        // "boolean"
console.log(typeof undefined);   // "undefined"
console.log(typeof null);        // "object" ← JavaScript bug! (historical)
console.log(typeof Symbol());    // "symbol"

Object Types — Complex Data

Objects key-value pairs मध्ये data store करतात. JavaScript मध्ये Arrays, Functions, Dates — सगळे objects आहेत.

Object Types

javascript
// Object — key-value pairs
let person = { name: "Rahul", age: 25, city: "Pune" };
person.age = 26; // Mutable — बदलता येते

// Array — ordered collection
let fruits = ["आंबा", "केळी", "द्राक्षे"];

// Function — callable object
function greet() { console.log("नमस्कार!"); }

// Date
let today = new Date();

Primitives vs Objects — मुख्य फरक

  • Primitives: Immutable (value बदलत नाही, नवीन value बनते) | Copied by VALUE
  • Objects: Mutable (property बदलता येते) | Copied by REFERENCE

Copy by Value vs Reference

javascript
// Primitive — Copy by Value
let a = 10;
let b = a;   // b मध्ये 10 चे copy
b = 20;
console.log(a); // 10 — a बदलला नाही!

// Object — Copy by Reference
let obj1 = { name: "Rahul" };
let obj2 = obj1;  // obj2 same memory location कडे point करतो
obj2.name = "Priya";
console.log(obj1.name); // "Priya" — obj1 पण बदलला! ⚠️

Key Points — लक्षात ठेवा

  • 7 Primitives: Number, String, Boolean, Null, Undefined, Symbol, BigInt
  • typeof operator ने type check करता येतो
  • Primitives: immutable, copied by value
  • Objects: mutable, copied by reference
  • null typeof = "object" — JavaScript चा historical bug
0/13 chapters पूर्ण