JavaScript Object Methods - Interview Cheat Sheet
This cheat sheet covers how to create, access, and manipulate objects in JavaScript, along with
commonly used Object methods - from beginner to advanced - with syntax, examples, and use
cases for interviews and coding challenges.
1) Object Creation Methods
// 1. Object Literal
const user = { name: "Pooja", age: 25 };
// 2. Using new Object()
const user = new Object();
[Link] = "Pooja";
// 3. Using Constructor Function
function User(name, age) {
[Link] = name;
[Link] = age;
}
const u1 = new User("Pooja", 25);
// 4. Using Class
class User {
constructor(name, age) {
[Link] = name;
[Link] = age;
}
}
const u2 = new User("Pooja", 25);
// 5. Using [Link]()
const baseUser = { type: "admin" };
const u3 = [Link](baseUser);
[Link] = "Pooja";
------------------------------------------------------------
2) Accessing Object Properties
// 1. Dot Notation
[Link]([Link]);
// 2. Bracket Notation
[Link](user["age"]);
// 3. Dynamic Access
const key = "name";
[Link](user[key]);
// 4. Optional Chaining
[Link](user?.address?.city);
------------------------------------------------------------
3) Common Object Methods
[Link]()
Use: Return array of keys.
Syntax: [Link](obj)
Example:
const user = {a:1, b:2}; [Link](user); // ['a','b']
Use Case: Used to loop over keys in iteration problems.
------------------------------------------------------------
[Link]()
Use: Return array of values.
Syntax: [Link](obj)
Example:
const user = {a:1, b:2}; [Link](user); // [1,2]
Use Case: Used for summing or filtering numeric values.
------------------------------------------------------------
[Link]()
Use: Return array of [key, value] pairs.
Syntax: [Link](obj)
Example:
const user = {a:1, b:2}; [Link](user); // [['a',1],['b',2]]
Use Case: Used for iterating or converting to Map.
------------------------------------------------------------
[Link]()
Use: Convert array of pairs back to object.
Syntax: [Link](pairs)
Example:
const arr = [['a',1],['b',2]]; [Link](arr); // {a:1,b:2}
Use Case: Used when transforming objects.
------------------------------------------------------------
[Link]()
Use: Copy properties between objects.
Syntax: [Link](target, source)
Example:
const a={x:1}, b={y:2}; [Link](a,b); // {x:1,y:2}
Use Case: Used for shallow cloning or merging.
------------------------------------------------------------
[Link]()
Use: Prevents any change to the object.
Syntax: [Link](obj)
Example:
const user={name:'Pooja'}; [Link](user); [Link]='A'; // no effect
Use Case: Used to create immutable configs or constants.
------------------------------------------------------------
[Link]()
Use: Prevents adding/removing properties.
Syntax: [Link](obj)
Example:
const user={name:'Pooja'}; [Link](user); [Link]=30; // no add
Use Case: Used when structure should stay same.
------------------------------------------------------------
[Link]()
Use: Checks if key exists directly on object.
Syntax: [Link](obj, key)
Example:
const obj={a:1}; [Link](obj,'a'); // true
Use Case: Used in validation and safe key checks.
------------------------------------------------------------
[Link]()
Use: Create new object from prototype.
Syntax: [Link](proto)
Example:
const admin={role:'admin'}; const u=[Link](admin); [Link]='Pooja';
Use Case: Used in prototype inheritance.
------------------------------------------------------------
4) Working with Array of Objects
const users = [
{ name: "Pooja", age: 25 },
{ name: "Amit", age: 30 },
{ name: "Riya", age: 22 }
];
// 1. Accessing
[Link](users[0].name); // 'Pooja'
// 2. Looping
[Link](u => [Link]([Link]));
// 3. Filtering
const adults = [Link](u => [Link] > 25);
// 4. Mapping
const names = [Link](u => [Link]);
// 5. Sorting
[Link]((a,b) => [Link] - [Link]);
// 6. Reducing (Average Age)
const avg = [Link]((a,b)=>a+[Link],0)/[Link];
------------------------------------------------------------
5) Advanced Object Techniques
// 1. Shallow vs Deep Copy
const shallow = [Link]({}, obj);
const deep = [Link]([Link](obj));
// 2. Destructuring
const {name, age} = user;
// 3. Spread Operator
const newUser = {...user, city: "Pune"};
// 4. Merge Multiple
const merged = {...obj1, ...obj2};
// 5. Dynamic Keys
const key = "email";
const user = { [key]: "pooja@[Link]" };
// 6. Convert Object <-> Array
const arr = [Link](obj);
const backToObj = [Link](arr);