We use object properties to store and get information about things. Accessing these properties lets us read or change that information easily.
Accessing object properties in Javascript
objectName.propertyName // or objectName["propertyName"]
You can use dot notation (objectName.propertyName) when you know the exact property name.
Use bracket notation (objectName["propertyName"]) when the property name is stored in a variable or has spaces/special characters.
name property using dot notation.const person = { name: "Alice", age: 30 }; console.log(person.name);
age property using bracket notation.const person = { name: "Alice", age: 30 }; console.log(person["age"]);
key with bracket notation to access a property.const key = "name"; const person = { name: "Alice", age: 30 }; console.log(person[key]);
This program shows three ways to access properties of the car object. It prints the brand, model, and year.
const car = { brand: "Toyota", model: "Corolla", year: 2022 }; console.log(car.brand); // Using dot notation console.log(car["model"]); // Using bracket notation const prop = "year"; console.log(car[prop]); // Using variable with bracket notation
If you try to access a property that does not exist, JavaScript returns undefined.
Bracket notation is useful when property names have spaces or special characters, like object["full name"].
Use dot notation for simple, known property names.
Use bracket notation when property names are dynamic or have special characters.
Accessing properties lets you read or change information stored in objects.
Practice
age in a JavaScript object person when the property name is known and simple?Solution
Step 1: Understand dot notation usage
Dot notation is used when the property name is a simple, valid identifier known in advance.Step 2: Apply dot notation to access property
Usingperson.ageaccesses theageproperty directly.Final Answer:
person.age -> Option BQuick Check:
Dot notation = person.age [OK]
- Using arrow (->) instead of dot
- Using parentheses like a function call
- Using bracket notation unnecessarily
"first-name" of an object user?Solution
Step 1: Identify property name with special characters
The property name"first-name"contains a hyphen, which is not valid for dot notation.Step 2: Use bracket notation with quotes
Bracket notation with quotes,user['first-name'], correctly accesses such properties.Final Answer:
user['first-name'] -> Option AQuick Check:
Bracket notation for special chars = user['first-name'] [OK]
- Using dot notation with hyphenated names
- Omitting quotes inside brackets
- Using undefined variable inside brackets
const car = { brand: 'Toyota', year: 2020 };
console.log(car['brand']);Solution
Step 1: Understand bracket notation access
The code uses bracket notation with the string 'brand' to access the property value.Step 2: Retrieve the value of 'brand'
The value ofcar['brand']is 'Toyota', so it prints 'Toyota'.Final Answer:
Toyota -> Option DQuick Check:
car['brand'] = 'Toyota' [OK]
- Confusing property name with value
- Expecting property name to print
- Using wrong quotes causing error
const book = { title: '1984', author: 'Orwell' };
console.log(book[title]);Solution
Step 1: Check bracket notation usage
Inside brackets, property names must be strings or variables. Here,titleis treated as a variable, not a string.Step 2: Fix by adding quotes
Adding quotes:book['title']correctly accesses the property.Final Answer:
Missing quotes around 'title' in bracket notation -> Option CQuick Check:
Bracket keys need quotes if not variables [OK]
- Forgetting quotes around string keys
- Assuming dot notation always required
- Confusing variable and string keys
settings = { 'theme-color': 'dark', fontSize: 14 }, which code correctly changes the theme-color to 'light' and increases fontSize by 2?Solution
Step 1: Update 'theme-color' using bracket notation
Since 'theme-color' has a hyphen, use bracket notation:settings['theme-color'] = 'light';Step 2: Increase fontSize using dot notation
fontSize is a simple property, sosettings.fontSize += 2;correctly adds 2.Final Answer:
settings['theme-color'] = 'light'; settings.fontSize += 2; -> Option AQuick Check:
Bracket for special keys + dot for simple keys [OK]
- Using dot notation for hyphenated keys
- Mistyping += as =+
- Mixing up property names and strings
Ready to prove it?
Pass the test and this pattern moves unknown
