JavaScript Objects

Properties, methods and this.

✍️ 3 exercises + code challenge in this lesson

An object groups related data and behaviour as key: value pairs.

  • Read a property with dot notation person.name or brackets person["name"].
  • Add or change properties by assigning: person.age = 30;
  • Delete with delete person.age;
  • A function stored in an object is a method. Inside it, this refers to the object.

Object.keys(), Object.values() and Object.entries() list an object's contents.

Key Takeaways: JavaScript Objects

Mastering JavaScript Objects helps you write clean and reliable JavaScript code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

JavaScript Objects Code Example

JavaScript
<pre id="out"></pre>
<script>
const log = (...a) => document.getElementById("out").textContent += a.join(" ") + "\n";
const person = {
  firstName: "Ayesha",
  lastName: "Malik",
  age: 26,
  fullName() {
    return this.firstName + " " + this.lastName;
  }
};

person.city = "Islamabad";
log(person.fullName(), "is", person["age"]);
log("Keys:", Object.keys(person).join(", "));

for (const [key, value] of Object.entries(person)) {
  if (typeof value !== "function") log(" -", key, "=", value);
}
</script>
Free sign-in required · Google login available

JavaScript Objects Practice Exercises

Check what you learned in “JavaScript Objects”.

/ 3 solved
Fill in the blank

Read the name property.

const user = { name: "Ali" };
log(user]);
✓ Correct! Not quite. Try again.
Multiple choice

Inside an object method, what does "this" refer to?

✓ Correct! Not quite. Try again.
Code challenge

Create an object car with brand "Toyota" and year 2020, then log car.brand.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services