What Are Objects?
In JavaScript, an object is a collection of key-value pairs, where each key (also called a property name) is associated with a value. Objects allow you to group related data and functionality together, making your code more organized and flexible. Objects are central to JavaScript, as almost everything in the language—arrays, functions, and even other objects—can be treated as objects. By using objects, you can represent real-world entities, store structured information, and define behaviors using methods.
1234567const user = { name: "Alice", age: 30, isMember: true }; console.log(JSON.stringify(user));
You can access the properties of an object using either dot notation or bracket notation. With dot notation, you write the object name, a dot, and the property name, like user.name. Bracket notation lets you use a string inside square brackets, such as user["age"]. Both notations allow you to read or update property values. For example, using the user object above, you can get the name with user.name or user["name"], and the age with user.age or user["age"]. Dot notation is typically preferred for property names that are valid identifiers, while bracket notation is useful when the property name is stored in a variable or contains characters not allowed in identifiers.
123456789101112131415const user = { name: "Alice", age: 30, isMember: true }; // Accessing properties console.log(user.name); // Dot notation console.log(user["age"]); // Bracket notation // Updating properties user.isMember = false; // Dot notation user["name"] = "Bob"; // Bracket notation console.log(JSON.stringify(user));
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 7.69
What Are Objects?
Stryg for at vise menuen
In JavaScript, an object is a collection of key-value pairs, where each key (also called a property name) is associated with a value. Objects allow you to group related data and functionality together, making your code more organized and flexible. Objects are central to JavaScript, as almost everything in the language—arrays, functions, and even other objects—can be treated as objects. By using objects, you can represent real-world entities, store structured information, and define behaviors using methods.
1234567const user = { name: "Alice", age: 30, isMember: true }; console.log(JSON.stringify(user));
You can access the properties of an object using either dot notation or bracket notation. With dot notation, you write the object name, a dot, and the property name, like user.name. Bracket notation lets you use a string inside square brackets, such as user["age"]. Both notations allow you to read or update property values. For example, using the user object above, you can get the name with user.name or user["name"], and the age with user.age or user["age"]. Dot notation is typically preferred for property names that are valid identifiers, while bracket notation is useful when the property name is stored in a variable or contains characters not allowed in identifiers.
123456789101112131415const user = { name: "Alice", age: 30, isMember: true }; // Accessing properties console.log(user.name); // Dot notation console.log(user["age"]); // Bracket notation // Updating properties user.isMember = false; // Dot notation user["name"] = "Bob"; // Bracket notation console.log(JSON.stringify(user));
Tak for dine kommentarer!