Introduction
An object is one of the fundamental concepts in JavaScript and Object-Oriented Programming (OOP). An object is a collection of properties (data) and methods (functions) that represent a real-world entity.
For example, a student object may contain properties like name, age, and course, along with methods such as study() or displayDetails().
Objects make code more organized, reusable, and easier to maintain by grouping related data and functionality together.
In Node.js, objects are used extensively to represent users, products, API responses, database records, configuration settings, and much more.
For automation engineers, objects are commonly used to store test data, API responses, page elements, configuration values, and reusable methods in frameworks like Playwright, Selenium, and Cypress.
In this tutorial, you’ll learn how to create and use objects in Node.js.
What is an Object?
An object is a collection of key-value pairs.
Each key is called a property, and its associated value can be:
A string
A number
A boolean
An array
Another object
A function (called a method)
Why Use Objects?
Objects help developers:
Organize related data
Store multiple values together
Improve code readability
Represent real-world entities
Reuse data and methods
Simplify application development
Support object-oriented programming
Object Syntax
const objectName = {
property: value
};
Example 1: Create a Simple Object
const employee = {
name: "John",
department: "IT"
};
console.log(employee);
Sample Output
{
name: 'John',
department: 'IT'
}
Example 2: Access Object Properties
Use dot notation.
const employee = {
name: "John",
department: "IT"
};
console.log(employee.name);
console.log(employee.department);
Sample Output
John
IT
Example 3: Add a New Property
const employee = {
name: "John"
};
employee.department = "IT";
console.log(employee);
Sample Output
{
name: 'John',
department: 'IT'
}
Example 4: Update a Property
const employee = {
name: "John"
};
employee.name = "Alice";
console.log(employee.name);
Sample Output
Alice
Example 5: Object with Methods
const employee = {
name: "John",
greet() {
console.log(
"Welcome " + this.name
);
}
};
employee.greet();
Sample Output
Welcome John
Example 6: Nested Objects
const employee = {
name: "John",
address: {
city: "Bangalore",
country: "India"
}
};
console.log(employee.address.city);
Sample Output
Bangalore
Example 7: Object with an Array
const student = {
name: "David",
subjects: [
"JavaScript",
"Node.js",
"React"
]
};
console.log(student.subjects);
Sample Output
[ 'JavaScript', 'Node.js', 'React' ]
Real-World Example
Represent a product using an object.
const product = {
name: "Laptop",
price: 75000,
brand: "Dell"
};
console.log(product.name);
console.log(product.price);
Sample Output
Laptop
75000
Automation Testing Example
Objects are widely used in automation frameworks.
Playwright Example
Store login credentials.
const credentials = {
username: "admin",
password: "admin123"
};
console.log(credentials.username);
Selenium Example
Store page element locators.
const loginPage = {
usernameField: "#username",
passwordField: "#password",
loginButton: "#login"
};
console.log(loginPage.loginButton);
Cypress Example
Store test configuration.
const config = {
baseUrl: "https://example.com",
browser: "chrome"
};
console.log(config.baseUrl);
API Testing Example
Store an API response.
const response = {
status: 200,
message: "Success"
};
console.log(response.message);
Data-Driven Testing Example
Store employee information.
const employee = {
id: 101,
name: "John",
department: "IT"
};
console.log(employee.id);
Common Mistakes
Using Dot Notation with a Non-Existing Property
const employee = {
name: "John"
};
console.log(employee.age);
Sample Output
undefined
Always verify that the property exists before using it.
Forgetting Quotes Around String Values
Incorrect:
const employee = {
name: John
};
Correct:
const employee = {
name: "John"
};
Forgetting Commas Between Properties
Incorrect:
const employee = {
name: "John"
department: "IT"
};
Correct:
const employee = {
name: "John",
department: "IT"
};
Best Practices
Use meaningful property names.
Group related data inside objects.
Use methods for object-specific behavior.
Keep objects focused on a single purpose.
Use nested objects only when necessary.
Use dot notation for readable property access.
Follow consistent formatting for better readability.
Conclusion
Objects are one of the most important building blocks of Node.js and Object-Oriented Programming. They allow developers to group related data and behavior into a single structure, making applications easier to understand and maintain.
For automation engineers, objects are used extensively to store test data, API responses, page locators, configuration settings, and reusable information.
Mastering objects is essential before learning advanced OOP concepts such as constructors, classes, inheritance, encapsulation, and polymorphism.
Frequently Asked Questions (FAQs)
What is an object?
An object is a collection of properties and methods that represent a real-world entity.
How do you access an object’s property?
Using dot notation.
Example:
employee.name;
Can an object contain another object?
Yes. Objects can contain nested objects.
Can an object contain functions?
Yes. Functions inside objects are called methods.
Why are objects important in automation testing?
Objects are used to store test data, API responses, page locators, configuration settings, and reusable information in automation frameworks such as Playwright, Selenium, and Cypress.
Key Takeaways
Objects store related data using key-value pairs.
Properties represent data, while methods represent behavior.
Access properties using dot notation.
Objects can contain arrays, nested objects, and methods.
Objects improve code organization and readability.
They are widely used to represent real-world entities.
Objects are heavily used in Playwright, Selenium, Cypress, API testing, and Node.js applications.
Use meaningful property names and keep objects focused on a single purpose.
Nested objects help organize complex data structures.
Mastering objects is essential for learning advanced Object-Oriented Programming concepts.
