An Object is a collection of "key-value" pairs. Instead of using a number to find data (like in an array), you use a name (a key). JSON (JavaScript Object Notation) is just a string format of this data used to send info over the internet.
1. Object Literals
Created using curly braces {}. Inside, you define a key: value.
Properties: The data stored (e.g.,
name: "Gemini").Methods: Functions stored inside an object (e.g.,
greet: function() { ... }).
2. Accessing Data
Dot Notation:
user.name(Most common).Bracket Notation:
user["name"](Useful if the key is stored in a variable).
3. JSON
The format almost every API uses. It looks exactly like a JS object but keys must be in double quotes.
The Code Example
// 1. Creating an Object
const smartPhone = {
brand: "Apple",
model: "iPhone 15",
batteryPct: 85,
is5G: true,
// 2. Adding a Method (A function inside an object)
ring: function() {
console.log("Ring ring! 🔔");
}
};
// 3. Accessing and Modifying
console.log(smartPhone.brand); // "Apple"
smartPhone.batteryPct = 90; // Updating a value
smartPhone.color = "Space Gray"; // Adding a new property
smartPhone.ring(); // Calling the method
// 4. JSON Conversion (Sending/Receiving data)
const jsonString = JSON.stringify(smartPhone); // Object to String
console.log(jsonString);
const backToObject = JSON.parse(jsonString); // String back to Object