JavaScript AcademyProfessional Engineering
Level 2 · Core DevelopmentModule 8: Objects and Structured DataLesson 36 of 150About 40 minutes

Lesson 36: Creating JavaScript objects

01Opening question

A customer has a name, an email, a city and a loyalty level. Should that be four separate variables, or one thing?

Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.

02Start with working code

An object groups related values under names called properties.

const customer = {
  name: "Ada",
  city: "London",
  points: 120
};
console.log(customer.name);
customer.points += 30;
console.log(customer);

03Objective and prerequisites

Learning objective: Create objects with literal syntax, read and update properties with dot notation, add and delete properties, and print objects clearly.

You need: Lesson 26: Creating and accessing arrays, Lesson 30: Building collection-based applications

Skills practised: Objects.

04Explanation

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.

05Definitions

Object
A collection of named values (properties).
Property
A key and its value inside an object.
Key
The name of a property.
Dot notation
Reading or writing a property with object.key.
Object literal
Creating an object directly with curly braces.

Open the full glossary

06Visual learning object

Object property inspector. Type an object and see every key, value and type, plus what Object.keys, values and entries return.

Loading the interactive object…

Open the full Object property inspector page with its guided experiment.

07Examples

ex-036-01Create and read Browser-executable JavaScript

Dot notation reads a property; console.log shows the whole object.

const book = { title: "Dune", author: "Frank Herbert", pages: 412 };
console.log(book.title);
console.log(book.pages > 300);
console.log(book);
Sample input
None
Expected output
Dune true {title: "Dune", author: "Frank Herbert", pages: 412}
Common mistake
Putting quotes around the property in dot notation: book."title".
Try this
Add a year property and print it.
Source
examples/ex-036-01-create-read.js (repository pending publication)

ex-036-02Update, add and delete Browser-executable JavaScript

Objects change in place, even with const.

const user = { name: "Grace", role: "viewer" };
user.role = "editor";
user.email = "grace@example.com";
delete user.email;
console.log(user);
console.log(user.email);
Sample input
None
Expected output
{name: "Grace", role: "editor"} undefined
Common mistake
Expecting const to prevent property changes.
Try this
Add a lastLogin property with a date string.
Source
examples/ex-036-02-update-add-delete.js (repository pending publication)

ex-036-03Shorthand properties Browser-executable JavaScript

When a variable has the same name as the key, write it once.

const name = "Lamp";
const price = 30;
const product = { name, price, inStock: true };
console.log(product);
Sample input
None
Expected output
{name: "Lamp", price: 30, inStock: true}
Common mistake
Writing { name: name } everywhere; shorthand is equivalent.
Try this
Create a product from three variables with shorthand.
Source
examples/ex-036-03-shorthand.js (repository pending publication)

ex-036-04Missing properties Browser-executable JavaScript

A missing property is undefined; going deeper throws.

const order = { id: 7, total: 40 };
console.log(order.discount);
console.log(order.discount === undefined);
console.log("total" in order, "discount" in order);
Sample input
None
Expected output
undefined true true false
Common mistake
Reading order.customer.name when there is no customer, which throws a TypeError.
Try this
Add a discount and run again.
Source
examples/ex-036-04-missing-property.js (repository pending publication)

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Fix the property name

Read the property that actually exists.

const product = { name: "Kettle", price: 24.99, stock: 3 };
console.log(`${product.name}: $${product.cost}`);

Profile updates

Update, add and delete properties.

const profile = { name: "Linus", city: "Helsinki", phone: "555-0100" };
// TODO: change city to "Portland", add email "linus@example.com", delete phone
console.log(profile);

Product factory

Create objects with a consistent shape from a function.

// Write createProduct(name, price, stock = 0)

09Prediction

Commit to an answer first, then run the real code to compare.

const a = { x: 1 };
a.y = a.x + 1;
delete a.x;
console.log(a, a.x);

What is printed?

10Debugging investigation

Loyalty points never update

After a purchase the customer should have 150 points, but the screen still shows 100.

const customer = { name: "Ada", points: 100 };
customer.point = customer.points + 50;
console.log("Points:", customer.points);

More cases in the Debugging Detective Laboratory

11Practical scenario

Customer records

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

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

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.

16AI learning prompts

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 explanation JavaScript 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 prediction JavaScript 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.
Debugging JavaScript 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 generation JavaScript 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 review JavaScript 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.