Object Flashcards

To learn about object manipulation, creation, deletion and methods (30 cards)

1
Q

What is an object in JavaScript?

A

A collection of key-value pairs used to store related data and functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create an object literal?

A

Use curly braces {}. Example: const person = { name: 'John', age: 30 };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you access an object property?

A

Use dot or bracket notation. Example: person.name or person['name'];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you add a new property to an object?

A

Assign a value to a new key. Example: person.job = 'Designer';

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you remove a property from an object?

A

Use the delete keyword. Example: delete person.age;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you check if an object has a property?

A

Use the in operator or .hasOwnProperty(). Example: 'name' in person or person.hasOwnProperty('name');

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you loop through object properties?

A

Use a for...in loop. Example: for (let key in person) { console.log(key, person[key]); }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you get all property names from an object?

A

Use Object.keys(obj). Example: Object.keys(person);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you get all property values from an object?

A

Use Object.values(obj). Example: Object.values(person);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you get both keys and values from an object?

A

Use Object.entries(obj). Example: Object.entries(person);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you merge multiple objects?

A

Use Object.assign(target, ...sources) or the spread operator {...obj1, ...obj2}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create a shallow copy of an object?

A

Use {...obj} or Object.assign({}, obj);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you freeze an object to make it immutable?

A

Use Object.freeze(obj);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you prevent adding or removing properties to an object?

A

Use Object.seal(obj);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you create a new object with a specific prototype?

A

Use Object.create(proto);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you get the prototype of an object?

A

Use Object.getPrototypeOf(obj);

17
Q

How do you define methods inside an object?

A

Use function expressions or shorthand syntax. Example: const person = { greet() { console.log('Hi'); } };

18
Q

What does this refer to inside an object method?

A

It refers to the object that the method belongs to.

19
Q

How do you convert an object to a JSON string?

A

Use JSON.stringify(obj);

20
Q

How do you parse a JSON string back to an object?

A

Use JSON.parse(jsonString);

21
Q

What is destructuring in objects?

A

Extracting values from objects into variables. Example: const { name, age } = person;

22
Q

How do you rename a property while destructuring?

A

Use a colon. Example: const { name: fullName } = person;

23
Q

How do you provide a default value during destructuring?

A

Use =. Example: const { age = 25 } = person;

24
Q

How do you check if two objects are equal?

A

Compare their keys and values manually or use JSON.stringify(obj1) === JSON.stringify(obj2);

25
What is the difference between a shallow and deep copy?
A shallow copy only copies the first level; a deep copy recursively copies nested objects.
26
How do you deep copy an object?
Use `structuredClone(obj)` or `JSON.parse(JSON.stringify(obj));`
27
How do you get the number of properties in an object?
Use `Object.keys(obj).length;`
28
How do you define getters and setters in an object?
Use `get` and `set` keywords. Example: `get name() { return this._name; } set name(n) { this._name = n; }`
29
How do you convert an array of key-value pairs into an object?
Use `Object.fromEntries(array);`
30
What is optional chaining?
It allows safe property access using `?.`. Example: `person.address?.city;` returns undefined if address doesn’t exist.