QuestenaPractice that shows what to review next
Topic lesson

Objects and properties: choose the right access and inspection rule

About 5 min

Questions stay in the language in which they were published.

These questions ask you to predict property access, distinguish direct ownership from inheritance, and identify what object inspection or copying produces. A short classification of the operation is more reliable than guessing from the property value.

Classify the property operation

First identify how the property name is formed. Dot access uses the identifier written after the dot: profile.city always asks for the property named city. Bracket access evaluates its expression first: if field contains city, profile[field] asks for that same property. Numbers used as ordinary property names are converted to strings, while Symbols remain Symbols.

GoalRule to apply
Read or write a propertyA read of an absent property produces undefined. Under normal writable conditions, assigning through an absent name creates a directly owned property; for example, profile.city = Lima stores that value on profile.
Test presence or ownershipThe in operator searches the object and its prototype chain. Object.hasOwn checks only the supplied object itself.
Enumerate ordinary visible dataObject.keys returns directly owned enumerable string names. Object.values returns their values, and Object.entries returns corresponding name-and-value pairs. Inherited and Symbol-named properties are omitted.
Copy with object spreadSpread copies enumerable directly owned properties one level deep. If a copied value is another object, both outer objects still refer to that same nested object; a later occurrence of the same name replaces an earlier one.
Access through a possibly nullish valueOptional chaining stops at its optional link when the left side is null or undefined and produces undefined. For example, missingUser?.city does not attempt the city access.

For deletion, keep the target separate from local variables. Deleting a configurable directly owned property removes that property and evaluates to a boolean status. It does not erase a lexical binding that happens to have the same spelling.

Where the mistakes come from

Tempting routeHow to reject it
Treating a number as a third property-name type, or treating dot access as a variable lookupOrdinary names end as strings or Symbols. A dot fixes the written identifier; only brackets evaluate an expression that can change which property is selected.
Turning an absent read into an exception, or assuming an absent property cannot be assignedA normal read completes with undefined, while an ordinary assignment can create the property. Errors from accessing through null or undefined are a different case.
Using one presence test for both inherited and directly owned propertiesDecide whether the prototype chain should count. Use in when it should, and Object.hasOwn when only the object itself should count.
Assuming enumeration includes inherited or Symbol-named data, or assuming spread recursively clones nested objectsThe common Object enumeration methods use directly owned enumerable string names, and spread is shallow. Check ownership, enumerability, name type, and depth separately.

Try it

Question 63

Which statements about bracket property access are true? Select all that apply.

  1. If key holds the string name, object[key] accesses the name property
  2. object.key always accesses the same property as object[key]
  3. Changing the value of key can make object[key] select another property
  4. The expression inside brackets must be a string literal
Question 68

base.rank is 1, and item inherits from base without defining rank itself. What does Object.hasOwn(item, "rank") return?

  1. 1, because it returns the inherited value
  2. undefined, because it returns the missing own value
  3. true, because inherited properties count as owned
  4. false, because rank is not item's own property
Question 72

The variable value is null. What does the expression value?.name produce?

  1. The string name
  2. The value null
  3. A TypeError
  4. The value undefined
Start drillPractice this topic
Objects and properties: choose the right access and inspection rule · Questena