Access Properties
Use dot notation for static keys and bracket notation for dynamic keys.
Bracket notation also allows keys with spaces or special characters.
JavaScript Tutorial
Object properties store values under keys, and you can access them using dot or bracket notation.
Understanding property access is essential for working with real data structures.
Most data in JavaScript is represented as objects with properties.
Knowing how to access and update properties prevents common bugs.
obj.key
obj['key']
delete obj.keyconst user = { name: "Ava", "role type": "admin" };
console.log(user.name);
console.log(user["role type"]);Dot for simple keys, bracket for special keys.
const key = "email";
const user = {};
user[key] = "a@example.com";
console.log(user.email);Use bracket notation for dynamic keys.
Use dot notation for static keys and bracket notation for dynamic keys.
Bracket notation also allows keys with spaces or special characters.
You can add new properties or update existing ones with simple assignment.
Objects are mutable by default.
Use delete to remove a property and in or hasOwn to check existence.
Prefer Object.hasOwn for own properties only.
Without
const value = obj['dynamicKey'];With
const value = obj[dynamicKey];Use bracket notation for variables as keys.
Prefer creating new objects when possible.
Check with in or Object.hasOwn before access.
Dot for static keys, bracket for dynamic or special keys.
Use delete obj.key.
Use in or Object.hasOwn.
Practice: Add a property called role to a user object and log it.
const user = { name: "Ava" };
// TODO: add role
One Possible Solution
const user = { name: "Ava" };
user.role = "admin";
console.log(user.role);When the key is dynamic or has special characters.
Use key in obj or Object.hasOwn(obj, key).
It removes only the property you target.
Try accessing properties with dot and bracket notation.