JavaScript Objects
What are Objects?
"Key-value pairs"
Objects are collections of properties (key-value pairs) in JavaScript.
"JavaScript objects are collections of key-value pairs, used to represent structured data and enable object-oriented programming."
Object Creation
Literal · Constructor · Object.create()
// Object literal (most common)
const obj = { name: "John", age: 30 };
// Constructor
const obj = new Object();
obj.name = "John";
// Object.create()
const obj = Object.create(null);
"Objects can be created with literals (most common), constructors, or Object.create()."
Property Access
Dot notation · Bracket notation
const obj = { name: "John", "key with spaces": "value" };
// Dot notation
obj.name; // 'John'
// Bracket notation
obj["name"]; // 'John'
obj["key with spaces"]; // 'value' (required for special keys)
"Properties accessed with dot notation or bracket notation - bracket required for special characters or dynamic keys."
Property Types
Data properties · Accessor properties
// Data property
const obj = {
name: "John", // value, writable, enumerable, configurable
};
// Accessor property
const obj = {
get name() {
return this._name;
},
set name(value) {
this._name = value;
},
};
"Properties can be data properties (value) or accessor properties (getter/setter)."
Property Descriptors
value · writable · enumerable · configurable
Object.defineProperty(obj, "name", {
value: "John",
writable: false, // Can't change
enumerable: true, // Shows in for...in
configurable: false, // Can't delete or reconfigure
});
"Property descriptors control value, writability, enumerability, and configurability."
Object Methods
Object.keys · values · entries · assign
const obj = { a: 1, b: 2 };
Object.keys(obj); // ['a', 'b']
Object.values(obj); // [1, 2]
Object.entries(obj); // [['a', 1], ['b', 2]]
Object.assign({}, obj); // Shallow copy
"Object methods include keys, values, entries for iteration, and assign for copying."
Object Destructuring
Extract properties easily
const obj = { name: "John", age: 30 };
// Destructuring
const { name, age } = obj;
// Rename
const { name: personName } = obj;
// Default values
const { name, city = "Unknown" } = obj;
"Destructuring extracts object properties into variables, with support for renaming and default values."
Spread Operator
Copy and merge objects
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3 };
// Copy
const copy = { ...obj1 };
// Merge
const merged = { ...obj1, ...obj2 }; // { a: 1, b: 2, c: 3 }
// Override
const updated = { ...obj1, b: 5 }; // { a: 1, b: 5 }
"Spread operator copies and merges objects, with later properties overriding earlier ones."
Reference Types
Objects are passed by reference
const obj1 = { name: "John" };
const obj2 = obj1; // Reference, not copy
obj2.name = "Jane";
console.log(obj1.name); // 'Jane' (both reference same object)
"Objects are reference types - assigning doesn't copy, both variables reference the same object."
Object Comparison
Reference equality, not value equality
const obj1 = { name: "John" };
const obj2 = { name: "John" };
obj1 === obj2; // false (different references)
obj1 === obj1; // true (same reference)
"Object comparison uses reference equality - two objects with same properties are not equal unless same reference."
🧠 Ultra-Short Cheat Sheet
Key-value pairs
Literal, constructor, Object.create()
Dot/bracket notation
Property descriptors
Reference types
Object methods (keys/values/entries)
Destructuring
Spread operator