𝗢𝗯𝗷𝗲𝗰𝘁𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁
JavaScript objects store data in key-value pairs. Each pair is a property. A property holds a value like a string, number, or array. If a property holds a function, you call it a method.
Methods describe what an object does.
How to create objects:
Object Literals Use curly braces to define data. let user = { name: "Sourav", age: 23 };
Object Constructor Use the new Object() command. let user = new Object(); user.name = "Sourav";
How to access data:
- Dot notation: user.name
- Bracket notation: user["name"]
You can change objects after you create them. You can add new properties or remove old ones.
To add a property: user.job = "Developer";
To remove a property, use the delete operator: delete user.age;
To check if a property exists, use the in operator or hasOwnProperty method: "name" in user; user.hasOwnProperty("name");
Objects group related data and logic together. This makes your code easier to manage.
Source: https://dev.to/kamalesh_ar_6252544786997/objects-in-javascript-2kc9