LESSON 19 · BEGINNER4 min read

Objects & Property Access

Group related data, read properties safely, and understand how object references are shared.

ES2024 / Node.js 20+Free to readModule 4 of 19
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Create object literals and choose between dot and bracket access.
  • Read nested properties with optional chaining and enumerate own keys.
  • Explain reference copying and the shallow behavior of Object.freeze.

01Named values in one place

An object groups related values under keys. An object literal uses braces and key-value pairs. Dot access, user.name, is shortest when the key is a fixed identifier. Bracket access, user['name'], accepts a variable or a key that is not a valid identifier. Both read the same property.

Missing properties return undefined. Distinguish a missing key from a key whose value is undefined by checking ownership with Object.hasOwn(user, 'name') or the in operator. Prefer Object.hasOwn for own properties; in also reports inherited properties. A typo in a property name is silent, so a small shape check saves debugging time. Reading is forgiving, but a typo on the left side of an assignment can silently create a new property instead of updating the intended one.

property-access.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

02Nest objects and call methods

Objects can contain other objects and arrays. Access follows the path: order.customer.address.city. Optional chaining, order.customer?.address?.city, short-circuits to undefined when a link is missing instead of throwing. A missing final property still returns undefined, so use a fallback when the display needs text. If the whole chain may be absent, put one optional marker at the point where uncertainty begins.

A function stored as a property is a method. In a regular method, this refers to the object used to call it, so this.total can read another property. Arrow functions do not get their own this, so they are a poor fit for methods that need the receiver. Keep methods small and prefer returning new objects when sharing becomes complicated.

nested-object.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

03Inspect keys, values, and entries

Object.keys returns an array of own enumerable keys, Object.values returns their values, and Object.entries returns [key, value] pairs. These arrays work with map, filter, and reduce, which makes them a bridge between objects and array pipelines. Iteration order follows insertion order for ordinary string keys.

for...of over Object.entries is a readable way to process every property. The in operator checks a key anywhere on the prototype chain. Optional chaining is for reading through a possibly missing object; it does not create missing properties. Use nullish coalescing, ??, when a fallback should apply only to null or undefined, not to 0 or an empty string.

entries.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

04References, copies, and shallow freeze

Assigning an object to another variable copies the reference, not the object. Both names point to the same properties, so changing one is visible through the other. Spread, { ...user }, creates a shallow copy: top-level properties are copied, but nested objects are still shared. Copy the nested value as well when the two objects must diverge.

Object.freeze prevents adding, removing, or reassigning top-level properties. It is shallow, so a frozen object can still contain a mutable nested array. Freeze nested values separately if you need deeper protection. For everyday work, avoid shared mutation by creating a new object at each update. A frozen object is still useful as a shared configuration value because accidental assignment cannot change it.

references.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
LET IT CLICK

The ideas worth keeping.

  • Objects group named values, and bracket access supports dynamic or non-identifier keys.
  • Optional chaining and nullish coalescing make nested reads safe without hiding real values.
  • Assignment copies references; spread and Object.freeze only protect the top level.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Read a city safely

Write cityOf(user) that returns user.address.city when it exists and 'Unknown' otherwise. It must handle a missing address and a null user without throwing. Test it with a full user, a user without an address, and null.

your_turn_1.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
function cityOf(user) {  return user?.address?.city ?? 'Unknown';}console.log(cityOf({ address: { city: 'Lagos' } }));console.log(cityOf({ name: 'Sam' }));console.log(cityOf(null));

Optional chaining stops at the first missing link and yields undefined instead of throwing. Nullish coalescing then supplies 'Unknown' for both undefined and null. The output is Lagos, Unknown, Unknown.

02

Exercise 2: Increment views without mutation

Write incrementViews(article) that returns a new object with views increased by one, treating a missing views value as 0. The original article must not change. Test it on { title: 'Scope', views: 4 }.

your_turn_2.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
function incrementViews(article) {  return {    ...article,    views: (article.views ?? 0) + 1,  };}const article = { title: 'Scope', views: 4 };const updated = incrementViews(article);console.log(updated);console.log(article.views);

Spread copies the existing properties into a new object, then the views key is replaced with the incremented value. The assignment creates a separate top-level object, so article.views remains 4. The output is { title: 'Scope', views: 5 } followed by 4.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01When is bracket access required instead of dot access?
02What does Object.freeze protect?
03After const copy = { ...original }, which statement is true?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

Exercise 1: Read a city safely

Write cityOf(user) that returns user.address.city when it exists and 'Unknown' otherwise. It must handle a missing address and a null user without throwing. Test it with a full user, a user without an address, and null.

EXPECTED OUTPUT
Lagos
Unknown
Unknown
main.jsJavaScript
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock lesson completion.