Creating Objects

Introduction

An object is one of the most important data types in JavaScript. Unlike arrays that store values using numeric indexes, objects store data as key-value pairs, making them ideal for representing real-world entities.

Objects allow you to group related information together. For example, instead of storing a person’s name, age, and city in separate variables, you can store them inside a single object.

Objects are widely used in web development, Node.js applications, API responses, JSON data, and automation frameworks such as Selenium, Playwright, and Cypress.

For automation engineers, objects are commonly used to store user information, browser configurations, API request data, test results, environment settings, and page element locators.


What is an Object?

An object is a collection of properties, where each property consists of:

  • A key (also called a property name)

  • A value

Example:

{
  name: "John",
  age: 25,
  city: "Bangalore"
}

In this object:

  • name is the key and "John" is its value.

  • age is the key and 25 is its value.

  • city is the key and "Bangalore" is its value.


Why Do We Use Objects?

Objects help developers:

  • Store related data together.

  • Represent real-world entities.

  • Improve code organization.

  • Make data easier to access.

  • Simplify API response handling.

  • Create reusable data structures.


Creating an Object Using Object Literal

The most common way to create an object is by using object literal syntax.

Syntax

let objectName = {

    key1: value1,

    key2: value2

};

Example

let student = {

    name: "John",

    age: 22,

    course: "JavaScript"

};

console.log(student);

Output

{ name: 'John', age: 22, course: 'JavaScript' }

Creating an Empty Object

You can create an empty object and add properties later.

let employee = {};

employee.name = "Alice";

employee.role = "Tester";

console.log(employee);

Output

{ name: 'Alice', role: 'Tester' }

Creating an Object Using the Object Constructor

JavaScript also provides the Object constructor.

let car = new Object();

car.brand = "Toyota";

car.model = "Camry";

console.log(car);

Output

{ brand: 'Toyota', model: 'Camry' }

Although this approach works, object literal syntax is generally preferred because it is simpler and more readable.


Objects Can Store Different Data Types

An object can contain strings, numbers, booleans, arrays, functions, and even other objects.

let person = {

    name: "David",

    age: 30,

    married: true,

    skills: ["JavaScript", "Node.js"]

};

console.log(person);

Output

{
  name: 'David',
  age: 30,
  married: true,
  skills: [ 'JavaScript', 'Node.js' ]
}

Real-World Example

Suppose an application stores employee information.

let employee = {

    id: 101,

    name: "John",

    department: "QA"

};

console.log(employee);

Output

{ id: 101, name: 'John', department: 'QA' }

Another example:

Store product details.

let product = {

    name: "Laptop",

    price: 65000,

    available: true

};

console.log(product);

Output

{ name: 'Laptop', price: 65000, available: true }

Automation Testing Example

Automation engineers frequently create objects to store test data, browser settings, API request information, and environment configurations.

Playwright Example

Store browser configuration.

const browserConfig = {

    browser: "chromium",

    headless: true

};

console.log(browserConfig);

Output

{ browser: 'chromium', headless: true }

Selenium Example

Store browser capabilities.

const capabilities = {

    browserName: "chrome",

    platform: "Windows"

};

console.log(capabilities);

Output

{ browserName: 'chrome', platform: 'Windows' }

Cypress Example

Store test user details.

const user = {

    username: "admin",

    password: "admin123"

};

console.log(user);

Output

{ username: 'admin', password: 'admin123' }

API Testing Example

Store request details.

const request = {

    method: "GET",

    endpoint: "/users"

};

console.log(request);

Output

{ method: 'GET', endpoint: '/users' }

Data-Driven Testing Example

Store login credentials.

const credentials = {

    username: "tester",

    password: "test123"

};

console.log(credentials);

Output

{ username: 'tester', password: 'test123' }

Common Mistakes

Using Square Brackets Instead of Curly Braces

Incorrect:

let person = [

    name: "John"

];

This produces a syntax error because arrays use square brackets and objects use curly braces.

Correct:

let person = {

    name: "John"

};

Forgetting the Colon (:)

Incorrect:

let student = {

    name = "John"

};

Correct:

let student = {

    name: "John"

};

Missing Commas Between Properties

Incorrect:

let employee = {

    name: "Alice"

    age: 28

};

Always separate properties with commas.


Best Practices

Use Object Literal Syntax

Object literals are simpler and easier to read than the Object constructor.


Use Meaningful Property Names

Instead of:

let person = {

    n: "John",

    a: 25

};

Use:

let person = {

    name: "John",

    age: 25

};

This improves code readability.


Group Related Information

Store related values inside the same object instead of creating multiple independent variables.


Keep Object Structure Consistent

When creating multiple objects of the same type, use the same property names for consistency.


Conclusion

Objects are one of the most powerful and commonly used data structures in JavaScript. They store data as key-value pairs, making it easy to represent real-world entities and organize related information.

For automation engineers, objects are essential for storing test data, browser configurations, API requests, user credentials, environment settings, and application data. Understanding how to create objects is the foundation for working with JSON, APIs, and modern JavaScript automation frameworks.


Frequently Asked Questions (FAQs)

What is an object in JavaScript?

An object is a collection of key-value pairs used to store related data.


What is the most common way to create an object?

Using an object literal.

let person = {

    name: "John"

};

Can objects store different data types?

Yes. Objects can store strings, numbers, booleans, arrays, functions, other objects, and more.


Can I create an empty object?

Yes.

let employee = {};

Is the Object constructor recommended?

It works, but object literal syntax is generally preferred because it is shorter and easier to read.


Why are objects important in automation testing?

Automation engineers use objects to store browser settings, test data, API requests, credentials, environment configurations, and page element locators in a structured and reusable format.


Key Takeaways

  • Objects store data as key-value pairs.

  • Objects are created using curly braces {}.

  • Object literals are the most common way to create objects.

  • Objects can contain multiple data types.

  • Empty objects can be created and updated later.

  • The Object constructor can also be used but is less common.

  • Objects help organize related information efficiently.

  • They are widely used in JavaScript, JSON, APIs, and automation testing.

  • Use meaningful property names to improve readability.

  • Mastering objects is essential for modern JavaScript development and test automation.