Objects & JSON

🟨 JavaScript Lesson 9 Intermediate

Objects are collection mappings storing key-value pairs. JSON (JavaScript Object Notation) is a lightweight data interchange format based on JavaScript object syntax.

1 Object Literals & Methods

Objects map text keys to any data value (including other nested objects and functions, which act as **object methods**). Accessing properties is done via Dot notation (`obj.key`) or Bracket notation (`obj["key"]`), which allows dynamic property evaluation.

2 Object Manipulation & JSON Transformations

Let's run a program initializing objects, running methods via the `this` keyword, and encoding to and from JSON strings:

JavaScript — Objects & JSON ▶ Run Code
const student = {
    name: "Alice",
    age: 21,
    skills: ["JS", "Node"],
    // Object Method
    introduce() {
        return `Hi, I am ${this.name} and I know ${this.skills.join(", ")}.`;
    }
};

console.log(student.introduce());

// JSON Conversions
const jsonString = JSON.stringify(student);
console.log("As JSON string: " + jsonString);

const parsedObject = JSON.parse(jsonString);
console.log("Parsed GPA (Dynamic field add):", parsedObject.name);
3 Code Challenge
Challenge: Create an object called `book` with properties for title, author, and year. Write a method on the book object that returns a formatted description. Convert this book object into a JSON string and print the output.