syntax Copy
const obj = { key: value };
const obj = Object.create (proto);example Copy
const user = {
name: "Kai" ,
age: 30 ,
greet() {
return `Hi, I'm ${this.name}` ;
}
};
console.log(user.greet());output
"Hi, I'm Kai"Note Method shorthand greet() {} is preferred over greet: function() {}. Arrow functions should not be used as methods because they do not bind their own this.
create object object literal define object object with methods
syntax Copy
obj.property
obj["property" ]
obj[variable]example Copy
const config = { "max-retries" : 3 , timeout: 5000 };
console.log(config.timeout);
console.log(config["max-retries" ]);
const key = "timeout" ;
console.log(config[key]); Note Use bracket notation for dynamic keys or keys with special characters. Dot notation is cleaner for static, valid-identifier keys.
access object property dot notation bracket notation dynamic property
Property Shorthand and Computed Keys syntax Copy
const obj = { name, [expression]: value };example Copy
const name = "Mira" ;
const role = "admin" ;
const user = { name, role };
console.log(user);
const field = "email" ;
const form = { [field]: "[email protected] " };
console.log(form); Note Shorthand works when the variable name matches the desired key name. Computed keys are evaluated at runtime.
shorthand property computed property key dynamic key variable as key
Object Destructuring in Function Parameters syntax Copy
function fn({ key1, key2 = default }) {}example Copy
function createUser({ name, role = "viewer" , active = true }) {
return { name, role, active, createdAt: Date.now() };
}
const admin = createUser({ name: "Bo" , role: "admin" });
console.log(admin);output
{ name: "Bo", role: "admin", active: true, createdAt: ... }Note Destructuring in parameters makes function signatures self-documenting. Add = {} as a default to allow calling with no arguments: function fn({ x } = {}).
destructure function params named parameters options object pattern
Object.keys / values / entries syntax Copy
Object.keys(obj)
Object.values (obj)
Object.entries(obj)example Copy
const inventory = { apples: 5 , bananas: 12 , cherries: 30 };
console.log(Object.keys(inventory));
console.log(Object.values (inventory));
for (const [fruit, count] of Object.entries(inventory)) {
console.log(`${fruit}: ${count}` );
}Note These only return own enumerable string-keyed properties. Symbol keys and inherited properties are excluded.
iterate object object keys object values loop over object object to array
syntax Copy
Object.assign(target, ...sources)
{ ...obj1, ...obj2 }example Copy
const defaults = { theme: "light" , lang: "en" , debug: false };
const userPrefs = { theme: "dark" , debug: true };
const settings = { ...defaults, ...userPrefs };
console.log(settings);output
{ theme: "dark", lang: "en", debug: true }Note Both approaches are shallow. Later sources overwrite earlier ones. Use structuredClone for deep merging or a library for recursive merge.
merge objects combine objects Object.assign spread merge extend object
Freezing and Sealing Objects syntax Copy
Object.freeze(obj)
Object.seal(obj)
Object.isFrozen(obj)example Copy
const API = Object.freeze({
BASE_URL: "https://api.example.com" ,
VERSION: 2 ,
});
API.VERSION = 3 ;
console.log(API.VERSION); Note freeze() prevents all changes. seal() allows modifying existing properties but not adding/removing. Both are shallow -- nested objects remain mutable.
freeze object immutable object prevent modification Object.freeze Object.seal
Checking Property Existence syntax Copy
Object.hasOwn(obj, prop)
"prop" in objexample Copy
const user = { name: "Lina" , age: 28 };
console.log(Object.hasOwn(user, "name" ));
console.log(Object.hasOwn(user, "email" ));
console.log("toString" in user);
console.log(Object.hasOwn(user, "toString" )); Note Object.hasOwn() (ES2022) replaces obj.hasOwnProperty(). It works even if the object was created with Object.create(null).
check property exists hasOwnProperty Object.hasOwn property check in operator
syntax Copy
Object.fromEntries(iterable)example Copy
const params = new URLSearchParams("page=2&sort=name&order=asc" );
const queryObj = Object.fromEntries(params);
console.log(queryObj);
const prices = { shirt: 25 , jacket: 120 , cap: 15 };
const discounted = Object.fromEntries(
Object.entries(prices).map(([item, price]) => [item, price * 0.9 ])
);
console.log(discounted);output
{ page: "2", sort: "name", order: "asc" }
{ shirt: 22.5, jacket: 108, cap: 13.5 }Note The inverse of Object.entries(). Takes any iterable of [key, value] pairs including Maps.
entries to object Object.fromEntries array to object map to object
syntax Copy
structuredClone(obj)example Copy
const original = {
user: { name: "Kai" , tags: ["admin" , "editor" ] },
updatedAt: new Date(),
};
const clone = structuredClone(original);
clone.user.tags.push("viewer" );
console.log(original.user.tags); Note Handles circular references, Dates, Maps, Sets, ArrayBuffers, and more. Does NOT clone functions, Errors, or DOM nodes.
deep clone object copy nested object structuredClone deep copy object