Objects
Deslize para mostrar o menu
Objects in JavaScript are used to store collections of data as key-value pairs. Each key is a string (or symbol), and each value can be any data type, including numbers, strings, arrays, or even other objects. You use objects when you want to represent a single entity with various properties, such as a user with a name, age, and membership status.
1234567891011121314151617181920// Creating an object with key-value pairs const user = { name: "Alice", age: 28, isMember: true }; // Accessing properties using dot notation console.log(user.name); // "Alice" // Accessing properties using bracket notation console.log(user["age"]); // 28 // Updating a property user.age = 29; console.log(user.age); // 29 // Adding a new property user.email = "alice@example.com"; console.log(user.email); // "alice@example.com"
Arrays and objects are both used to group data, but they serve different purposes:
- Arrays are ordered lists accessed by numeric indices and are ideal for collections of similar items, such as a list of names;
- Objects, on the other hand, are unordered and use named keys, making them better for representing structured data where each value is associated with a specific property name.
To access or update object properties, you can use either dot notation (object.property) or bracket notation (object["property"]). Dot notation is more concise and commonly used when the property name is a valid identifier. Bracket notation is useful when the property name is dynamic or not a valid identifier.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo