JavaScript: objects
An object groups related data under named properties, making a value easier to describe and use.
What you will learn
- How to create and read object properties
- When dot or bracket notation is useful
- Why assigning an object copies a reference
Minimal example
const user = { name: 'Mina', age: 20 };
console.log(user.name); // "Mina"
user['age'] = 21;
user.active = true;Use dot notation for a known property name. Use bracket notation when the property name is stored in a variable or contains characters that dot notation cannot express.
Common mistakes
- Reading a missing property returns
undefined; check the data before using it. - Two variables assigned to the same object refer to the same object; changing one can affect the other.
- Use arrays for ordered lists and objects for named properties; they can also be nested when the data calls for it.