What is an Object?
An [object] is a container for grouped values. Unlike arrays which use indexes (0, 1, 2...), objects store data using a key-value format. This makes objects ideal for representing entities with properties, such as a user profile or a product details sheet.
To learn more, check the official documentation at MDN Objects Guide.
---
Creating and Manipulating Objects
Objects are created using curly braces {}. Each property consists of a key (or name) and a corresponding value.
#### Code Example: Defining Objects
let smartPhone = {
brand: "Apple",
model: "iPhone 17",
storageGB: 256,
is5GEnabled: true,
specificationSummary: function() {
return ${this.brand} ${this.model} with ${this.storageGB}GB;
}
};
// Access properties using dot notation
console.log("Model Name:", smartPhone.model);
// Access properties using bracket notation
console.log("Storage:", smartPhone["storageGB"]);
// Call an object method
console.log(smartPhone.specificationSummary());
#### Visual Output Demonstration:
Model Name: iPhone 17
Storage: 256
Apple iPhone 17 with 256GB
---
Introduction to JSON
[JSON] (JavaScript Object Notation) is a lightweight format used to send and receive data across web servers. It is heavily based on JavaScript's object syntax, but is formatted purely as text.
We convert JS objects to JSON strings using JSON.stringify(), and convert JSON strings back to JS objects using JSON.parse().
#### Code Example: Parsing and Stringifying
let userAccount = {
username: "coder2026",
status: "Active"
};
// Convert object to JSON string for network transmission
let jsonString = JSON.stringify(userAccount);
console.log("JSON Type:", typeof jsonString);
console.log("JSON String:", jsonString);
// Parse a JSON string back into a JS Object
let parsedObject = JSON.parse(jsonString);
console.log("Object Type:", typeof parsedObject);
console.log("Status Property:", parsedObject.status);
#### Visual Output Demonstration:
JSON Type: string
JSON String: {"username":"coder2026","status":"Active"}
Object Type: object
Status Property: Active
---
Frequently Asked Questions (FAQs)
#### Q1: What is the 'this' keyword inside an object method?
* The this keyword refers to the current object scope. Inside a method, this allows you to access other properties defined within that same object (e.g., this.brand inside our product example).
#### Q2: Can objects be nested inside other objects?
* Yes! You can nest objects and arrays inside other objects to build highly complex, nested data structures.
#### Q3: Why do we need to convert objects to JSON strings?
* Computers cannot directly send JavaScript memory objects over networks. Converting them to JSON strings formats them into a universal text format that any system or backend language can read, process, and convert back.
