Introduction
After creating an object, the next step is to access its properties. Accessing properties means retrieving the values stored inside an object using their property names.
JavaScript provides two ways to access object properties:
Dot Notation (
.)Bracket Notation (
[])
Both methods are widely used in JavaScript development, Node.js applications, API testing, and automation frameworks such as Selenium, Playwright, and Cypress.
For automation engineers, accessing object properties is essential when working with JSON responses, browser configurations, test data, environment variables, page objects, and API request or response data.
What are Object Properties?
An object stores information as key-value pairs.
Example:
let employee = {
name: "John",
department: "QA",
experience: 5
};
In this object:
namedepartmentexperience
are the properties.
The values associated with these properties can be accessed whenever needed.
Why Do We Access Object Properties?
Accessing properties helps developers:
Retrieve stored information.
Display object values.
Perform calculations.
Validate API responses.
Read configuration settings.
Process application data.
Accessing Properties Using Dot Notation
Dot notation is the most commonly used method.
Syntax
objectName.propertyName
Example
let student = {
name: "Alice",
age: 21,
course: "JavaScript"
};
console.log(student.name);
console.log(student.course);
Output
Alice
JavaScript
Accessing Properties Using Bracket Notation
Bracket notation uses the property name as a string.
Syntax
objectName["propertyName"]
Example
let employee = {
name: "John",
department: "QA"
};
console.log(employee["name"]);
console.log(employee["department"]);
Output
John
QA
When to Use Bracket Notation
Bracket notation is useful when the property name is stored in a variable.
let person = {
name: "David",
city: "Bangalore"
};
let property = "city";
console.log(person[property]);
Output
Bangalore
Accessing Non-Existing Properties
If a property does not exist, JavaScript returns undefined.
let product = {
name: "Laptop"
};
console.log(product.price);
Output
undefined
Accessing Nested Object Properties
Objects can contain other objects.
let employee = {
name: "John",
address: {
city: "Bangalore",
state: "Karnataka"
}
};
console.log(employee.address.city);
Output
Bangalore
Real-World Example
Suppose an application stores customer information.
let customer = {
id: 101,
name: "Rahul",
city: "Hyderabad"
};
console.log(customer.name);
console.log(customer.city);
Output
Rahul
Hyderabad
Another example:
Store mobile phone details.
let mobile = {
brand: "Samsung",
model: "Galaxy S24",
price: 75000
};
console.log(mobile.brand);
console.log(mobile.price);
Output
Samsung
75000
Automation Testing Example
Automation engineers frequently access object properties while reading API responses, browser settings, environment variables, and test data.
Playwright Example
Access browser configuration.
const browserConfig = {
browser: "chromium",
headless: true
};
console.log(browserConfig.browser);
Output
chromium
Selenium Example
Access browser capabilities.
const capabilities = {
browserName: "chrome",
platform: "Windows"
};
console.log(capabilities.browserName);
Output
chrome
Cypress Example
Access login credentials.
const user = {
username: "admin",
password: "admin123"
};
console.log(user.username);
Output
admin
API Testing Example
Access API response data.
const response = {
status: 200,
message: "Success"
};
console.log(response.status);
console.log(response.message);
Output
200
Success
Data-Driven Testing Example
Access test data.
const testData = {
username: "tester",
password: "test123"
};
console.log(testData.username);
Output
tester
Comparison of Property Access Methods
| Method | Syntax | Best Use Case |
|---|---|---|
| Dot Notation | object.property | When the property name is known. |
| Bracket Notation | object["property"] | When the property name is stored in a variable or contains special characters. |
Common Mistakes
Forgetting Quotes in Bracket Notation
Incorrect:
let person = {
name: "John"
};
console.log(person[name]);
If name is not defined as a variable, JavaScript throws a ReferenceError.
Correct:
console.log(person["name"]);
Using Dot Notation with Dynamic Property Names
Incorrect:
let property = "city";
console.log(person.property);
This looks for a property literally named "property".
Correct:
console.log(person[property]);
Accessing Non-Existing Properties
console.log(person.salary);
Output
undefined
Always verify that the property exists before using it.
Best Practices
Use Dot Notation Whenever Possible
It is shorter, cleaner, and easier to read.
Use Bracket Notation for Dynamic Properties
If the property name comes from user input or another variable, use bracket notation.
Use Meaningful Property Names
Instead of:
let employee = {
n: "John",
d: "QA"
};
Use:
let employee = {
name: "John",
department: "QA"
};
This improves readability.
Check for Missing Properties
If there is a possibility that a property does not exist, validate it before using it to avoid unexpected undefined values.
Conclusion
Accessing object properties is a fundamental JavaScript skill that allows developers to retrieve stored information efficiently. JavaScript provides two methods for property access: dot notation and bracket notation, each suited for different situations.
For automation engineers, accessing object properties is essential when working with JSON responses, browser configurations, API requests, environment variables, and test data. Mastering these techniques leads to cleaner, more reliable, and maintainable automation scripts.
Frequently Asked Questions (FAQs)
What are object properties?
Object properties are key-value pairs that store data inside an object.
What are the two ways to access object properties?
Dot notation (
.)Bracket notation (
[])
Which method is used most often?
Dot notation is the most commonly used because it is simple and readable.
When should I use bracket notation?
Use bracket notation when the property name is stored in a variable or contains spaces or special characters.
What happens if a property does not exist?
JavaScript returns:
undefined
Why is accessing object properties important in automation testing?
Automation engineers use object property access to read JSON responses, browser settings, API request details, environment variables, user credentials, and other structured data required for test automation.
Key Takeaways
Objects store data as key-value pairs.
Properties can be accessed using dot notation or bracket notation.
Dot notation is the preferred method for known property names.
Bracket notation is useful for dynamic property names.
Accessing a non-existing property returns
undefined.Nested object properties can be accessed using multiple dots.
Property access is widely used in JSON, APIs, and automation testing.
Use meaningful property names to improve readability.
Always validate property existence when necessary.
Mastering property access is essential for effective JavaScript programming and test automation.
