Learning objective: Create objects with literal syntax, read and update properties with dot notation, add and delete properties, and print objects clearly.
An object groups related values under names: { name: "Ada", points: 120 }. Each name-value pair is a property; the name is the key.
Read a property with dot notation: customer.name. Change it by assigning: customer.points = 150;.
Add a new property just by assigning to it: customer.email = "ada@example.com";. Remove one with delete customer.email;.
Reading a property that does not exist gives undefined, not an error. Reading a property of undefined is an error: customer.address.city fails if there is no address.
Object keys are strings or symbols; { 1: "a" } has the key "1". Property order follows insertion order for string keys, with integer-like keys first in ascending order.
Shorthand properties ({ name, city }) and computed keys ({ [field]: value }) keep object literals concise.
const objects can still be mutated. Object.freeze(obj) prevents changes (shallowly); in strict mode, writes to a frozen object throw.
Prefer a consistent shape: create objects with all their properties up front (even with null values) instead of adding properties later. Engines optimise stable shapes and code is easier to reason about.
A loyalty app stores each customer as an object with the same properties. Screens read properties by name, and updates change one property without touching the rest.
const c = { name: "Ada", tier: "silver", points: 820 };
if (c.points >= 800) c.tier = "gold";
console.log(`${c.name} is ${c.tier}`);
12Knowledge check
1. What is printed?
const u = { name: "Ada" };
console.log(u.name);
Ada
name
undefined
An error
2. What is printed?
const u = { name: "Ada" };
console.log(u.age);
null
undefined
An error
""
3. Can you add a property to an object declared with const?
No
Yes: const prevents reassigning the variable, not changing the object
Only with delete
Only numbers
4. Why does this throw TypeError: Cannot read properties of undefined?
const order = { id: 1 };
console.log(order.customer.name);
order is undefined
order.customer is undefined
name is a reserved word
Dot notation is wrong
5. What is printed?
const x = 1, y = 2;
console.log({ x, y });
{x: 1, y: 2}
{x: x, y: y}
An error
[1, 2]
6. Which is the better way to keep a customer's details?
Four variables: name, email, city, points
One object with four properties
An array of four strings
A single string
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the property name: Prints the price
Profile updates: Prints the profile; phone removed
Product factory: Prints both products; Rejects a bad price; Same keys every time
ex-036-01: output matches the documented expected output
ex-036-02: output matches the documented expected output
ex-036-03: output matches the documented expected output
ex-036-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-08-objects-and-structured-data/lesson-036-creating-javascript-objects/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Objects group related values under keys.
Read and write with dot notation.
Assign to add, delete to remove.
Missing properties are undefined; going deeper throws.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationJavaScript objects
Context: I am learning JavaScript objects in JavaScript.
Task: Explain objects, keys and dot notation with a customer record example.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionJavaScript objects drill
Context: I have practised JavaScript objects.
Task: Give me six snippets that read, add, change and delete properties and ask what each prints.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
DebuggingJavaScript objects bug
Context: My code prints undefined for a property I am sure I set.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationJavaScript objects practice
Context: I finished the JavaScript objects exercises.
Task: Create three exercises that build and update objects for a shop.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewJavaScript objects review
Context: Here is my code that uses JavaScript objects.
Task: Review how my code creates objects and suggest a consistent shape.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.