# Object Methods & Property Descriptors

> Unlock the hidden configuration of objects. Learn how to iterate over keys, freeze data, and tweak invisible property flags.

Subject: JavaScript | Lesson 21 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/object-methods-and-descriptors
Previous: [Destructuring, Spread & Rest](https://codewithanimation.in/javascript/destructuring-and-spread.md) | Next: [The Prototype Chain](https://codewithanimation.in/javascript/prototype-chain.md)

## The Object Utility Methods

Because objects aren't arrays, you can't just `map()` or `filter()` them directly. Instead, JavaScript provides static methods on the global `Object` class to extract their data into arrays.

`Object.keys(obj)` returns an array of the keys.

`Object.values(obj)` returns an array of the values.

`Object.entries(obj)` returns an array of `[key, value]` arrays, perfect for looping.

## Freezing and Sealing

Sometimes you need to lock down an object to prevent other code from modifying it.

`Object.freeze(obj)` makes an object completely immutable. You cannot add, delete, or change any properties.

`Object.seal(obj)` is slightly less strict. You cannot add or delete properties, but you *can* still change the values of existing properties.

## Property Descriptors

Under the hood, every property has hidden configuration flags called 'descriptors'. You can view them using `Object.getOwnPropertyDescriptor(obj, 'key')`.

The flags are: `writable` (can the value change?), `enumerable` (will it show up in loops/keys?), and `configurable` (can it be deleted or have its flags changed?).

## Common mistake: Silent failures on frozen objects

If you try to change a property on an `Object.freeze()` object in normal mode, JavaScript simply ignores the change and fails silently! It will only throw a `TypeError` if you are using `'use strict'`.

## Tip: Shallow freezing

Just like shallow copying, `Object.freeze()` is shallow. If your frozen object contains a nested array or object, that inner structure can still be mutated!

## Try it: Descriptor Anatomy

Analyze how Object methods extract data, and see the hidden descriptor flags that power properties.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/object-methods-and-descriptors

## Check yourself

### Question 1

Which method should you use if you want to loop through both the keys and values of an object?

- A. Object.entries()
- B. Object.keys()
- C. Object.values()

Correct answer: A

`Object.entries(obj)` gives you an array of `[key, value]` pairs, making it perfect for looping over objects.

- Why not B: Incorrect. This only returns the keys.
- Why not C: Incorrect. This only returns the values.

### Question 2

What is the difference between Object.freeze() and Object.seal()?

- A. seal allows changing existing properties, freeze does not.
- B. freeze allows adding new properties, seal does not.
- C. seal deep-freezes objects, freeze is only shallow.

Correct answer: A

Both stop you from adding or deleting keys. However, `seal` allows you to update existing values, while `freeze` locks the values entirely.

- Why not B: Incorrect. Neither allows adding new properties.
- Why not C: Incorrect. Both are shallow operations.

### Question 3

What does the `enumerable` descriptor flag do?

- A. It determines if the property shows up during loops like Object.keys().
- B. It determines if the property can be changed.
- C. It converts the property to a number.

Correct answer: A

If `enumerable` is false, the property is hidden from `for...in` loops and methods like `Object.keys()`, even though it still exists on the object.

- Why not B: Incorrect. That is what the `writable` flag does.
- Why not C: Incorrect. Descriptors don't change data types.

## Remember this

- `Object.keys()`, `values()`, and `entries()` are the standard ways to extract data from objects into arrays.
- `Object.freeze()` makes an object completely immutable (shallowly).
- `Object.seal()` prevents adding/deleting keys, but allows updating existing values.
- Property Descriptors hide flags like `writable`, `enumerable`, and `configurable` behind every property.
