iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Object Properties

Object properties have more attributes than just value. writable, enumerable, and configurable control how they behave — usually invisibly.

The four attributes (data properties)

AttributeDefault for…What it controls
valueThe actual value.
writabletrue via literalCan be reassigned?
enumerabletrue via literalShown by for…in, Object.keys, spread.
configurabletrue via literalCan be deleted or reconfigured?

Inspecting and defining

JS
const user = { name: "Ada" };

Object.getOwnPropertyDescriptor(user, "name");
// { value: "Ada", writable: true, enumerable: true, configurable: true }

Object.defineProperty(user, "id", {
  value: 42,
  writable:     false,   // can't reassign
  enumerable:   false,   // hidden from Object.keys / for…in / JSON.stringify
  configurable: false,   // can't delete or redefine
});

user.id;            // 42
user.id = 99;       // silently ignored (throws in strict mode)
"id" in user;       // true
Object.keys(user);  // ["name"]   ← id hidden

Property tests

JS
"name" in user                    // true — own or inherited
Object.hasOwn(user, "name")       // true — own only (modern)
user.hasOwnProperty("name")       // true — own only (older, can be shadowed)
typeof user.name === "string"     // value test, not existence

The three "lock" levels

MethodEffect
Object.preventExtensions(obj)Can't add new properties; existing ones still mutable.
Object.seal(obj)+ can't delete; properties become non-configurable.
Object.freeze(obj)+ can't write existing values. Fully read-only (shallow).
Tip: Reach for Object.freeze on configuration objects you pass around — it stops bugs that silently mutate shared state.

Example

Example
<!DOCTYPE html>
<html>
<body>

<p id="out"></p>

<script>
document.getElementById("out").textContent = "Hello from Object Properties!";
</script>

</body>
</html>
Try it Yourself »

Exercise

Make an object completely read-only.

const config = Object. (raw);

Test yourself

Q1. Hide a property from Object.keys with…
Q2. Modern own-property check is…
Q3. Object.freeze prevents…

Discussion

Loading…