Stage 2: Frontend Fundamentals

JavaScript · Lesson 05

Objects

Object literals, properties, destructuring, spread syntax and JSON.

JavaScript3 min readFree lesson

Learning Goals

  • Structure data with object literals.
  • Access properties with dot notation and bracket notation.
  • Understand methods and this.
  • Work with arrays of objects.
  • Use destructuring and the spread operator.
  • Understand the concept of JSON.

1. Object Literals

// Creating an object
const person = {
  name: "John Doe",     // property (key: value)
  age: 25,
  city: "New York",
  isStudent: false,
};

2. Accessing Properties

Dot Notation

console.log(person.name); // "John Doe"
console.log(person.age);  // 25

// Adding and updating properties
person.email = "[email protected]"; // add a new property
person.age = 26;                   // update an existing property

// Deleting a property
delete person.isStudent;

Bracket Notation

// Useful when the property name is dynamic
const key = "name";
console.log(person[key]); // "John Doe"

// Property names containing spaces
const obj = { "first name": "John" };
console.log(obj["first name"]); // "John"

3. Methods and this

A function defined inside an object is called a method.

const person = {
  name: "John Doe",
  age: 25,

  // Defining a method
  greet: function() {
    return `Hello, I'm ${this.name}!`; // this = the current object
  },

  // Shorthand method syntax (ES6+)
  getInfo() {
    return `${this.name} (age ${this.age})`;
  },
};

console.log(person.greet());   // "Hello, I'm John Doe!"
console.log(person.getInfo()); // "John Doe (age 25)"

Note: Arrow functions do not have their own this, so use regular functions for methods.


4. Arrays of Objects

This is the pattern you will use most often in real projects.

const students = [
  { id: 1, name: "Chris Kim", grade: "A", score: 95 },
  { id: 2, name: "Emily Lee", grade: "B", score: 82 },
  { id: 3, name: "Michael Park", grade: "A", score: 91 },
];

// Finding a specific student
const student = students.find(s => s.id === 2);
console.log(student.name); // "Emily Lee"

// Extracting only students with an A grade
const aStudents = students.filter(s => s.grade === "A");

// Calculating the average score (using reduce)
const avg = students.reduce((sum, s) => sum + s.score, 0) / students.length;
console.log(avg); // 89.33...

5. Destructuring

Destructuring makes it easy to extract an object's properties into variables.

const person = { name: "John Doe", age: 25, city: "New York" };

// The old way
const name1 = person.name;
const age1 = person.age;

// Destructuring
const { name, age, city } = person;
console.log(name, age, city); // "John Doe" 25 "New York"

// Renaming variables
const { name: userName, age: userAge } = person;
console.log(userName); // "John Doe"

// Setting default values
const { email = "none" } = person;
console.log(email); // "none" (default when the property is missing)

// Using destructuring in function parameters
function greet({ name, age }) {
  return `${name} is ${age} years old.`;
}
greet(person); // "John Doe is 25 years old."

6. Spread Operator

const original = { name: "John Doe", age: 25 };

// Copying an object (shallow copy)
const copy = { ...original };
copy.age = 30;
console.log(original.age); // 25 (the original is unaffected)

// Merging objects (later values win)
const extra = { city: "New York", age: 30 };
const merged = { ...original, ...extra };
// { name: "John Doe", age: 30, city: "New York" }

// Adding and updating properties
const updated = { ...original, email: "[email protected]", age: 26 };

7. JSON (JavaScript Object Notation)

JSON is the format used to exchange data as strings.

const person = { name: "John Doe", age: 25 };

// Object → JSON string (serialization)
const json = JSON.stringify(person);
console.log(json);        // '{"name":"John Doe","age":25}'
console.log(typeof json); // "string"

// JSON string → object (deserialization)
const parsed = JSON.parse(json);
console.log(parsed.name); // "John Doe"

// Pretty-printing (2-space indentation)
console.log(JSON.stringify(person, null, 2));

Practice Files

  • View Example
  • Check Assignment
  • Assignment Solution

Assignment

Assignment 05 — Address Book App

Assignment Goal

Build an address book app using arrays of objects, destructuring, and the spread operator.


Requirements

Data Structure

Each contact is an object with the following properties:

{
  id: 1,
  name: "John Doe",
  phone: "555-123-4567",
  email: "[email protected]"
}

Features

  1. Add: Enter a name, phone number, and email, then add the contact
  2. Delete: Remove a contact with the delete button on each contact
  3. Search: Live search by name or email (using filter)
  4. Details: Show all information when a contact is clicked (using find + destructuring)

Initial Data

const contacts = [
  { id: 1, name: "John Doe", phone: "555-111-2222", email: "[email protected]" },
  { id: 2, name: "Sarah Lee", phone: "555-333-4444", email: "[email protected]" },
  { id: 3, name: "David Kang", phone: "555-555-6666", email: "[email protected]" },
];

Validation Rules

  • Name is required.
  • The phone number should follow the XXX-XXX-XXXX format. (Hint: the 000-000-0000 pattern)
  • The email must contain an @ character.
  • Contacts with a duplicate name cannot be added.

Implementation Guide

Adding a Contact with the Spread Operator

// Copy the existing array while adding a new item
contacts = [...contacts, { id: nextId++, name, phone, email }];

Reading Form Data with Destructuring

const { name, phone, email } = getFormValues(); // destructure the function's return value

Searching with filter

const results = contacts.filter(c =>
  c.name.includes(keyword) || c.email.includes(keyword)
);

File Structure

assignment/
└── index.html   ← write your code here

Grading Criteria

Item Points
Structuring the data as an array of objects 15
Adding contacts (using the spread operator) 25
Search feature (using filter) 20
Delete feature 15
Displaying details (find + destructuring) 15
Input validation 10

Total: 100 points

Learn this lesson with a mentor

Work through the Build Your Homepage series on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now