JavaScript Object Properties
A literal and a dot read are already in your toolkit. On a real form the bag itself changes: a color shows up after the shopper picks one, a draft flag vanishes on save, and a city sits inside an address. This page is that everyday work on properties.
Past the first literal
The objects page built the bag and read a field. Here you change which keys exist: add one that was missing, drop one you no longer need, ask whether a name is present, step into an object stored inside another, and list the names you actually hold.
Five moves on a property
📊 Add, remove, test, nest, list
| Move | Syntax | What you get |
|---|---|---|
| add | item.color = "blue" |
a new property on the same object |
| remove | delete item.draft |
that key is gone |
| exists | "sku" in item |
true or false |
| nest | user.address.city |
a field inside another object |
| list | Object.keys(user) |
an array of the object's own keys |
Name the move and the line follows. Adding does not mint a second object. Removing does not turn the value into an empty string. A list of names is not the same job as reading one value.
let item = {
name: "Mug",
price: 8
};
item.color = "blue";
console.log(item.color);
Assignment grows a missing key
item began with name and price only. Because color was absent, item.color = "blue" creates it. You still have one object, now with three properties. The console prints blue.
let item = {
name: "Mug",
price: 8,
draft: true
};
delete item.draft;
console.log("draft" in item);
delete lifts the key off
delete item.draft removes that property, not the whole object. Afterwards "draft" in item is false. Name and price remain. Storing "" or null is a different act: the key stays, with a new value.
let item = {
name: "Mug",
price: 8
};
console.log("name" in item);
console.log("sku" in item);
in answers yes or no about the key
"name" in item is true — that name is on the object. "sku" in item is false — you never added it. A missing property reads back as undefined, and a stored undefined reads the same way. in asks about the key, not the value sitting there.
let user = {
name: "Lina",
address: {
city: "Shiraz",
zip: "71345"
}
};
console.log(user.address.city);
A property may hold another object
address is not a string. It is an object with city and zip. user.address.city takes two steps: into address, then into city. The console prints Shiraz. Nesting keeps the address pieces together instead of flattening every one onto user.
let user = {
name: "Lina",
role: "editor"
};
let names = Object.keys(user);
console.log(names);
Object.keys hands you names
Object.keys(user) returns an array of that object's own keys — here ["name", "role"]. You still read a value with a dot or with brackets. The list helps when you do not want every name hard-coded. It does not print the values by itself.
Choose the move, then type
A new field? Assign it. A key that should disappear? delete. A yes or no? in. A city inside an address? Nest, then walk the dots. Only the names? Object.keys. Functions stored on the object — methods — belong to the next lesson.
Put a nested city on the page after an add
Start with a user and an address, add plan in the script, and place the city in a paragraph.
<!DOCTYPE html>
<html>
<head>
<title>object-properties — page</title>
</head>
<body>
<p id="place">…</p>
<script>
let user = {
name: "Lina",
address: { city: "Shiraz" }
};
user.plan = "yearly";
let box = document.getElementById("place");
box.textContent = user.address.city + " — " + user.plan;
</script>
</body>
</html>
The paragraph shows Shiraz — yearly. The city was already nested; plan arrived after the literal. Change only city or plan, refresh, and read the new line. Next lesson: object methods — functions that live on the object.
Practice steps
- Build an object with two properties, add a third by assignment, and log it.
- Set a
draftproperty, remove it withdelete, and check"draft" in item. - On an object that only has
name, log"name" in itemand"sku" in item. - Nest an
addresswithcity, then loguser.address.city. - Log
Object.keysfor a two-key object and read the array. - In the HTML demo, change only
cityorplan, refresh, and read the paragraph.
Frequent slip-ups
- Thinking
item.color = "blue"mints a brand-new object — it grows the same one - Writing
item.draft = nullwhen you meantdelete item.draft— the key remains - Treating a missing key and a stored
undefinedas the same question — useinfor the key - Writing
user.citywhencitylives onuser.address - Expecting
Object.keysto return values - Hunting for methods and
thison this page — that is the next lesson
Summary
- Assigning to a missing key adds that property
deleteremoves the key; the rest of the object stays"key" in objtells you whether the key exists- A property can hold another object; extra dots walk inside
Object.keysreturns the names, not the values- A nested field and a newly added field can share one line on the page
- Next: object methods — functions stored on the object
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top