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)
| Attribute | Default for… | What it controls |
|---|---|---|
value | — | The actual value. |
writable | true via literal | Can be reassigned? |
enumerable | true via literal | Shown by for…in, Object.keys, spread. |
configurable | true via literal | Can 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
| Method | Effect |
|---|---|
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);
Six letters — temperature-related.
Discussion
Loading…