Episode 1 — Fundamentals / 1.23 — Objects

1.23 — Exercise Questions: JavaScript Objects

Practice questions for all six subtopics in Section 1.23. Mix of short-answer, prediction, code-writing, and debugging tasks.

How to use this material (instructions)

  1. Read lessons in orderREADME.md, then 1.23.a1.23.f.
  2. Answer closed-book first — then compare to the matching lesson.
  3. Write actual code — use the browser Console or Node.js to verify.
  4. Practice problems1.23.g-Practice-Problems.md.
  5. Interview prep1.23-Interview-Questions.md.
  6. Quick review1.23-Quick-Revision.md.

1.23.a — What is an Object? (Q1–Q10)

Q1. Define a JavaScript object in one sentence.

Q2. Write an object literal with three properties: title, year, and isPublished.

Q3. What is the difference between a primitive and a reference type? Name two examples of each.

Q4. Predict the output:

let a = { x: 1 };
let b = a;
b.x = 99;
console.log(a.x);

Q5. What does typeof {} return? What about typeof []?

Q6. Why does {} === {} evaluate to false?

Q7. What is autoboxing? Give an example with a string.

Q8. When should you use an object instead of an array?

Q9. What does typeof null return, and why is that considered a bug?

Q10. Write a function isPlainObject(value) that returns true only for plain objects, not arrays or null.


1.23.b — Key-Value Pairs (Q11–Q20)

Q11. What type are object keys internally — number or string?

Q12. Predict the output:

const name = "Alice";
const age = 25;
const user = { name, age };
console.log(user);

Q13. What is a computed property name? Write an example.

Q14. Which of these keys require quotes in an object literal?

  • firstName
  • full name
  • content-type
  • class
  • 123abc

Q15. What are Symbol keys used for? Do they appear in Object.keys?

Q16. Write an object with a method greet using method shorthand.

Q17. Why should you NOT use an arrow function for an object method that uses this?

Q18. Predict the output:

const obj = {
  value: 42,
  getValue: () => this.value,
};
console.log(obj.getValue());

Q19. What is the difference between greet: function() {} and greet() {} in an object?

Q20. Write an object with properties of five different types (string, number, boolean, array, nested object).


1.23.c — Accessing Properties (Q21–Q32)

Q21. What is the difference between dot notation and bracket notation?

Q22. When is bracket notation required?

Q23. Predict the output:

const user = { name: "Alice" };
console.log(user.age);

Q24. What operator prevents TypeError when accessing nested properties on null/undefined?

Q25. Predict the output:

const obj = { a: { b: { c: 42 } } };
console.log(obj.a?.b?.c);
console.log(obj.x?.y?.z);

Q26. What is the difference between || and ???

Q27. Predict the output:

const val1 = 0 || "default";
const val2 = 0 ?? "default";
console.log(val1, val2);

Q28. Write code to access a property using a variable as the key.

Q29. How do you call a method that might not exist using optional chaining?

Q30. Predict the output:

const obj = { a: undefined };
console.log(obj.a);
console.log(obj.b);
console.log("a" in obj);
console.log("b" in obj);

Q31. Write a function safeGet(obj, key, defaultValue) that returns the property value or the default if the property is nullish.

Q32. Can you use optional chaining with bracket notation? Write the syntax.


1.23.d — Adding and Deleting Properties (Q33–Q42)

Q33. Can you add properties to an object declared with const? Why or why not?

Q34. Write code that adds a phone property to a user object using bracket notation.

Q35. What does the delete operator return?

Q36. Predict the output:

const obj = { a: 1, b: 2 };
obj.a = undefined;
delete obj.b;
console.log("a" in obj);
console.log("b" in obj);
console.log(Object.keys(obj));

Q37. What is the difference between "key" in obj and Object.hasOwn(obj, "key")?

Q38. Why is Object.hasOwn preferred over obj.hasOwnProperty?

Q39. Write code that creates a non-mutating update of an object using the spread operator.

Q40. How do you remove a property immutably (without mutating the original)?

Q41. What is Object.assign and does it mutate the target?

Q42. Predict the output:

const a = { x: 1, y: 2 };
const b = { y: 3, z: 4 };
const c = { ...a, ...b };
console.log(c);

1.23.e — Nested Objects (Q43–Q52)

Q43. What error do you get when you access obj.foo.bar and obj.foo is undefined?

Q44. Write a safe way to access user.address.city when address might not exist.

Q45. What is a shallow copy? Which properties are shared?

Q46. Predict the output:

const original = { name: "Alice", address: { city: "Portland" } };
const copy = { ...original };
copy.address.city = "Seattle";
console.log(original.address.city);

Q47. Name two ways to create a deep copy of an object.

Q48. What types does structuredClone NOT support?

Q49. Name three limitations of JSON.parse(JSON.stringify(obj)) for deep copying.

Q50. Predict the output:

const obj = { date: new Date("2024-01-15"), count: NaN };
const copy = JSON.parse(JSON.stringify(obj));
console.log(typeof copy.date);
console.log(copy.count);

Q51. Write the spread-at-every-level pattern to update state.user.address.city to "Seattle" immutably.

Q52. When would you use structuredClone vs the spread operator?


1.23.f — Looping Through Keys (Q53–Q64)

Q53. What does for...in iterate over? Does it include prototype properties?

Q54. How do you guard against inherited properties in a for...in loop?

Q55. What does Object.keys(obj) return?

Q56. What does Object.values(obj) return?

Q57. What does Object.entries(obj) return?

Q58. Write a for...of loop with destructuring using Object.entries.

Q59. How do you convert an array of [key, value] pairs back into an object?

Q60. Predict the output:

const obj = { b: 2, a: 1, 2: "two", 1: "one", c: 3 };
console.log(Object.keys(obj));

Q61. Write a function that checks if an object is empty (no own properties).

Q62. Write code that filters an object to only include properties whose values are numbers.

Q63. Write a pick(obj, keys) function that returns a new object with only the specified keys.

Q64. Write an omit(obj, keys) function that returns a new object without the specified keys.


Mixed / Integration (Q65–Q70)

Q65. Given an array of objects, write code that creates a lookup object keyed by id:

const users = [
  { id: 1, name: "Alice" },
  { id: 2, name: "Bob" },
];
// Expected: { 1: { id: 1, name: "Alice" }, 2: { id: 2, name: "Bob" } }

Q66. Write a function deepEqual(obj1, obj2) that compares two objects recursively (handles nested objects).

Q67. Predict the output:

const a = { name: "Alice" };
const b = { name: "Alice" };
const c = a;
console.log(a === b);
console.log(a === c);
console.log(JSON.stringify(a) === JSON.stringify(b));

Q68. Write code that counts how many times each word appears in a string, storing results in an object.

Q69. Given a nested config object, write a function that returns a flat version with dot-separated keys:

// Input: { a: { b: 1, c: { d: 2 } }, e: 3 }
// Output: { "a.b": 1, "a.c.d": 2, "e": 3 }

Q70. Write a function groupBy(array, keyProp) that groups array items by a property value:

// Input: [{ dept: "eng", name: "A" }, { dept: "hr", name: "B" }, { dept: "eng", name: "C" }], "dept"
// Output: { eng: [...], hr: [...] }

Answer hints

QHint
Q499 — both variables reference the same object
Q5Both return "object"
Q6Different references in memory
Q12{ name: "Alice", age: 25 } — shorthand
Q18undefined — arrow functions do not bind this
Q23undefined — accessing non-existent property
Q27"default", 0?? only treats null/undefined as nullish
Q30undefined, undefined, true, false
Q36true, false, ["a"]
Q42{ x: 1, y: 3, z: 4 } — later spread wins
Q46"Seattle" — shallow copy shares nested references
Q50"string", null — JSON loses Date type and converts NaN to null
Q60["1", "2", "b", "a", "c"] — numeric keys sorted first
Q67false, true, true

← Back to 1.23 — JavaScript Objects (README)