Interview question
Explain JavaScript data types. What is the difference between primitive and object types? JavaScript के data types समझाएं। Primitive और object types में अंतर?
Answer
| Type | Category | Examples | Mutable? |
|---|---|---|---|
| String, Number, Boolean | Primitive | 'hello', 42, true | No |
| undefined, null, Symbol, BigInt | Primitive | undefined, null, Symbol('id'), 123n | No |
| Object, Array, Function | Object | {}, [], function(){} | Yes |
// Primitive: passed by value
let a = 5;
let b = a;
b = 10;
console.log(a); // 5 (unchanged)
// Object: passed by reference
let obj1 = { name: 'John' };
let obj2 = obj1;
obj2.name = 'Jane';
console.log(obj1.name); // 'Jane' (changed!)Interview tip: Primitives are immutable and passed by value; objects are mutable and passed by reference. This is why typeof null === 'object' (a famous quirk).
| Type | Category | Examples | Mutable? |
|---|---|---|---|
| String, Number, Boolean | Primitive | 'hello', 42, true | नहीं |
| undefined, null, Symbol, BigInt | Primitive | undefined, null, Symbol('id') | नहीं |
| Object, Array, Function | Object | {}, [], function(){} | हाँ |
// Primitive: value से pass होता है
let a = 5;
let b = a;
b = 10;
console.log(a); // 5 (unchanged)
// Object: reference से pass होता है
let obj1 = { name: 'John' };
let obj2 = obj1;
obj2.name = 'Jane';
console.log(obj1.name); // 'Jane' (changed!)इंटरव्यू टिप: Primitives immutable हैं और value से pass होते हैं; objects mutable हैं और reference से pass होते हैं। यह समझाएं typeof null === 'object' एक famous quirk है।
Was this answer clear?