DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

5 Important Points About Objects in JavaScript

Understand JavaScript objects through property access, ownership, prototype lookup, enumeration rules, and the limits of Object.freeze().

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JavaScript objects are more than bags of values: property lookup can reach a prototype, enumeration methods include different sets of keys, and freezing an object does not freeze objects nested inside it. These five points explain how to access, inspect, and constrain object properties without relying on assumptions from other languages.

1. An object is a collection of properties

A property associates a key with a value. In this object literal, name is an own property of user:

As an Amazon Associate I earn from qualifying purchases.

const user = { name: "Rae" };

The value can be a string, another object, or a function. A function stored on an object is commonly called a method. See MDN’s guide to working with objects.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

2. Dot and bracket notation access properties differently

Use dot notation when the property name is a valid identifier and known in the code. Bracket notation accepts a string or a key held in a variable, so it works for dynamic names and names that cannot be written as dot-notation identifiers.

const user = { name: "Rae", "favorite color": "blue" };
const field = "name";

user.name;              // "Rae"
user["name"];           // "Rae"
user[field];             // "Rae"
user["favorite color"]; // "blue"

Bracket notation is especially useful when data determines which property to read. An object’s prototype can also be chosen when creating it: Object.create(proto) creates an object whose prototype is proto; passing null creates one with no prototype.

3. A property can be found without being owned

Reading a property follows the prototype chain. If the object does not have the requested property itself, JavaScript checks its prototype and continues upward. Most ordinary objects inherit from Object.prototype, so a property can be accessible even when it is not a direct property.

const user = { name: "Rae" };

"toString" in user;              // true: found through the prototype chain
Object.hasOwn(user, "toString"); // false: not an own property

Use Object.hasOwn(obj, key) when the question is whether the object itself has a property. The in operator answers a broader question: whether the property exists on the object or anywhere in its prototype chain. An own property can also shadow an inherited property with the same name. MDN explains property ownership and enumerability.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Object.hasOwn is also safer than calling obj.hasOwnProperty(key): an object may have its own property named hasOwnProperty, hiding that inherited method.

4. Enumeration methods return different sets of properties

Choosing how to inspect an object depends on three questions: do you want own or inherited properties, enumerable or non-enumerable properties, and string keys or symbols? These methods do not all answer the same question.

Method Own or inherited Enumerable or non-enumerable Key types
Object.keys(obj) Own only Enumerable only String keys
Object.values(obj) Own only Enumerable only Values for own string-keyed properties
Object.entries(obj) Own only Enumerable only Pairs for own string-keyed properties
for...in Own and inherited Enumerable only String keys
Reflect.ownKeys(obj) Own only Enumerable and non-enumerable String and symbol keys

For a plain record’s enumerable own string keys, Object.keys is often the right choice. A for...in loop can also visit enumerable properties inherited from prototypes. If using it but you want only direct properties, check each key with Object.hasOwn:

for (const key in obj) {
  if (Object.hasOwn(obj, key)) {
    // Work with this own enumerable string key.
  }
}

Symbols and non-enumerable properties are outside the scope of Object.keys, Object.values, and Object.entries. Use Reflect.ownKeys when you need every own string and symbol key, or use symbol- and descriptor-specific APIs when you need to inspect a particular category. See MDN’s enumeration reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

5. Freezing an object does not freeze its nested objects

Object.freeze(obj) prevents adding or removing properties and changing existing property descriptors or data-property values on that object. It does not recursively freeze objects referenced by its properties.

const settings = {
  display: { theme: "light" }
};

Object.freeze(settings);
settings.display.theme = "dark"; // The nested object is still mutable.

The outer object is frozen, but settings.display still refers to a separate object that remains mutable. If nested objects also need to be frozen, each must be frozen separately; a single call does not make an entire object tree deeply immutable. For the method’s behavior and limitations, see MDN’s Object.freeze() reference.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.