𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗢𝗯𝗷𝗲𝗰𝘁𝘀: 𝗕𝗮𝘀𝗶𝗰𝘀 𝘁𝗼 𝗖𝗥𝗨𝗗
JavaScript objects store data and logic together. You use them to group related information into one place.
Think of an object as a real-world entity.
An entity is a thing you can identify. A student is an entity. An object has two parts:
- State: What the object has. (Name, Age)
- Behavior: What the object does. (Study, Attend Class)
In code, state is called a property. Behavior is called a method.
How to create objects
You have two main ways to build an object.
Object Literal This uses curly braces. It is the most common method. It is short and clean. let student = { name: "Saravanan", age: 25 };
Object Constructor This uses the new keyword. let student = new Object(); student.name = "Saravanan";
Most developers use the literal method because it is easier to read.
The CRUD Operations
You manage data in objects using four basic actions:
- Create: Define a new object.
- Read: Access data using dot notation (student.name) or bracket notation (student["name"]).
- Update: Change a value (student.age = 26).
- Delete: Remove a property using the delete keyword (delete student.age).
Advanced Object Features
Methods A function inside an object is a method. You can use shorthand to write them. let person = { greet() { console.log("Hello"); } };
Nested Objects An object can hold another object. This is a nested object. let person = { address: { city: "Chennai", state: "Tamil Nadu" } }; You access it like this: person.address.city.
Objects can store many types:
- Strings
- Numbers
- Booleans
- Arrays
- Functions
- Other objects
Source: https://dev.to/dev_saravanan_journey/javascript-objects-from-basics-to-crud-operations-46bg