mc0de
JavaScript · Lesson 09 of 17

JavaScript Objects

Give your data structure and meaning! Learn how JavaScript objects store related information, connect properties to values, and power real-world features like student profiles and product cards.

Properties Dot Notation Methods Nested Objects
● ● ●   objects.js
const student = {
  name: "Alex",
  course: "JavaScript",
  lessons: 9
};

console.log(student.name);
OBJECT EXPLORER
> Ready to inspect…
01 / Getting Started

What Is a JavaScript Object?

Imagine you’re building a website for a school. Each student has a name, an age, a course, and an enrollment status. Those details all describe one person, so it makes sense to keep them together.

A JavaScript object stores related information as key-value pairs, also called properties and values. Each key identifies a piece of information, while its value holds the actual data.

Creating your first object
const student = {
  name: "Alex",
  age: 22,
  course: "JavaScript",
  enrolled: true
};

console.log(student.name);
Console output
Alex

Objects use curly braces {}. Inside, each property has a key followed by a colon and a value. Commas separate the properties.

Arrays vs. objects: Arrays are great for ordered lists of items. Objects are great for describing something using named properties.
02 / Accessing Properties

Read Information With Dot & Bracket Notation

Once an object contains information, you’ll need a way to retrieve it. JavaScript provides two common approaches: dot notation and bracket notation.

Reading object properties
const student = {
  name: "Alex",
  course: "JavaScript",
  lessons: 17
};

console.log(student.name);
console.log(student["course"]);

// Use a variable to select a property
const property = "lessons";
console.log(student[property]);
Console output
Alex JavaScript 17

Dot notation is convenient when you know the property name. Bracket notation is useful when the property name comes from a variable or contains spaces or special characters.

For example, student[property] looks up the property whose name is stored in the property variable.

Watch out: student.property looks for a key literally named property. It does not substitute the variable’s value.
03 / Updating & Adding Properties

Change Object Information

Objects can be updated as information changes. You can modify an existing property or add a completely new one using assignment.

Editing a student profile
const student = {
  name: "Alex",
  course: "HTML"
};

student.course = "JavaScript";
student.level = "Beginner";

console.log(student);
Console output
{ name: “Alex”, course: “JavaScript”, level: “Beginner” }

We changed the course property and added a new level property. This makes objects useful for applications where information needs to change over time.

You can also remove a property using the delete operator.

Removing a property
delete student.level;

console.log(student.level);
Console output
undefined

Just like arrays, objects declared with const can still have their properties updated. The variable itself cannot be reassigned to a different object.

04 / Object Methods

Give Your Objects Actions

An object can store more than information. It can also contain functions that perform actions. When a function is stored as an object property, we commonly call it a method.

Creating a method
const student = {
  name: "Alex",
  course: "JavaScript",

  introduce() {
    return "Hi! I'm " + this.name +
      " and I'm learning " + this.course + ".";
  }
};

console.log(student.introduce());
Console output
Hi! I’m Alex and I’m learning JavaScript.

The method introduce() uses this.name and this.course to access properties on the object that receives the method call.

Methods help keep related data and behavior together. For example, a shopping cart object might contain its items and a method that calculates the total price.

Good habit: For beginner object methods that need this, use the regular method syntax shown above. Arrow functions handle this differently.
05 / Nested Objects & Arrays

Organize More Detailed Information

Real applications often need to store more than a few simple properties. An object can contain another object, an array, or a combination of both.

A more detailed student profile
const student = {
  name: "Alex",
  course: "JavaScript",

  contact: {
    city: "Irvine",
    email: "alex@example.com"
  },

  skills: ["HTML", "CSS", "JavaScript"]
};

console.log(student.contact.city);
console.log(student.skills[0]);
Console output
Irvine HTML

The contact property contains another object, while skills contains an array. You can use dot notation to access nested properties and array indexes to access list items.

These combinations are especially useful for student records, product catalogs, user profiles, and other structured information.

06 / Exploring Object Properties

Use Object.keys(), Object.values() & Object.entries()

Sometimes you don’t know every property name in advance. JavaScript provides methods that help you inspect and loop through an object’s own enumerable properties.

Exploring an object
const student = {
  name: "Alex",
  course: "JavaScript",
  level: "Beginner"
};

console.log(Object.keys(student));
console.log(Object.values(student));
Console output
[“name”, “course”, “level”] [“Alex”, “JavaScript”, “Beginner”]

Object.keys() returns an array of property names. Object.values() returns an array of their values.

Another method, Object.entries(), returns pairs of keys and values that are convenient to use in a loop.

Looping through an object’s properties
for (const [key, value] of Object.entries(student)) {
  console.log(key + ": " + value);
}
Console output
name: Alex course: JavaScript level: Beginner

The square brackets in [key, value] use destructuring to unpack each two-item entry into separate variables.

07 / Objects in Real Websites

Turn Object Data Into a Student Profile

Objects are especially useful when a webpage needs to display information about a person, product, event, or account.

Instead of keeping each detail in an unrelated variable, you can store everything in one object and use it to update the page.

Displaying a profile
const student = {
  name: "Alex",
  course: "JavaScript",
  level: "Beginner"
};

document.getElementById("studentName")
  .textContent = student.name;

document.getElementById("studentCourse")
  .textContent = student.course;

With matching HTML elements, JavaScript reads the object’s properties and displays them. If the object changes, you can update the webpage again to show the latest information.

Try a Live Object Explorer

Choose a property to see its value, or update the student’s course.

Choose a property and click Read Property.
Good habit: When showing information supplied by users, use textContent rather than treating it as HTML. It helps prevent unexpected markup from being interpreted by the browser.
🚀 mC0de Challenge

Build a Student Profile Builder

Create an interactive webpage where visitors can enter their information, save it in a JavaScript object, and display a personalized student profile.

Your Mission

  1. Create a student object containing a name, course, and skill level.
  2. Add text inputs or dropdowns for visitors to edit those properties.
  3. Create an Update Profile button.
  4. When clicked, update the object’s properties using the form values.
  5. Display the updated profile on the webpage.
  6. Use a reusable function to render the profile.
  7. Test different names, courses, and skill levels.
Extra challenge: Add a nested contact object containing a city, an array of skills, and a method that introduces the student. Then add a button to display the introduction.
💻 Interactive JavaScript Sandbox

Build Your Student Profile!

Edit the student object, customize the form, and run your code. The starter lets you update a name, course, and level. Select Load Solution to add a city, skills, and an introduction method.

Live Result
Lesson 09 Recap

What You’ve Learned

You’re Building Structured JavaScript!

You’ve learned how objects keep related information organized and make your applications easier to understand and maintain.

  • Create objects using properties and values.
  • Read properties with dot and bracket notation.
  • Update, add, and remove properties.
  • Build object methods using functions.
  • Work with nested objects and arrays.
  • Explore properties using Object.keys(), Object.values(), and Object.entries().
  • Create an interactive Student Profile Builder.

Next up: Strings — learn how to work with text, format messages, and transform user input!

Your Learning Journey

0 / 17 Completed

Keep going! Every completed lesson brings you closer to mastering JavaScript.

🎉

Nice Work!

You've learned how to organize related information with JavaScript objects, read and update properties, create methods, and work with nested data. You've even built your own interactive Student Profile Builder!