Object
Key → Value mapping (unordered, named access)
Objects and arrays are the core data structures of JavaScript. Almost every real-world application—from APIs to UI rendering—depends on them.
Understanding how they behave in memory is critical for writing bug-free code.
Object
Key → Value mapping (unordered, named access)
Array
Indexed list (ordered, numeric access)
const user = { id: 101, name: "Asha", active: true,};user.name;user["active"];const scores = [80, 92, 75, 99];scores[0]; // 80const user = { name: "Asha" };
// Createuser.age = 22;
// Readuser.name;
// Updateuser.name = "Riya";
// Deletedelete user.age;const arr = [1, 2];
// Createarr.push(3);
// Readarr[0];
// Updatearr[1] = 10;
// Deletearr.pop();for (let key in user) { console.log(key, user[key]);}arr.forEach((value) => console.log(value));Object.keys(obj);Object.values(obj);Object.entries(obj);arr.map(fn);arr.filter(fn);arr.reduce(fn);arr.find(fn);arr.some(fn);arr.every(fn);[1, 2, 3] .map((x) => x * 2) // [2,4,6] [(1, 2, 3)].filter((x) => x > 1) // [2,3] [(1, 2, 3)].reduce((a, b) => a + b, 0); // 6const user = { name: "Riya", orders: [ { id: 1, price: 100 }, { id: 2, price: 200 }, ],};const user = { name: "Sahil", age: 20 };
const { name, age } = user;const [a, b] = [10, 20];const arr = [1, 2];const newArr = [...arr, 3];function sum(...nums) { return nums.reduce((a, b) => a + b, 0);}const a = { x: 1 };const b = a;
b.x = 10;
console.log(a.x); // 10// for objectsconst copy = { ...obj }; // shallowconst deepCopy = JSON.parse(JSON.stringify(obj)); // deep
// for arraysconst arrCopy = [...arr]; // shallowconst deepArrCopy = arr.map((item) => ({ ...item })); // deep for array of objectsconst arr = [1, 2];const copy = arr;
copy.push(3);Both point to same memory.