Deleting Properties

Introduction

Sometimes, an object contains properties that are no longer required. JavaScript provides the delete operator to remove properties from an object.

Deleting a property permanently removes the property and its value from the object. Once deleted, attempting to access that property returns undefined.

The delete operator is commonly used in JavaScript applications, Node.js, API testing, and automation frameworks such as Selenium, Playwright, and Cypress.

For automation engineers, deleting properties is useful when removing unnecessary test data, cleaning API request payloads, updating configuration objects, and preparing dynamic test scenarios.


What is Deleting a Property?

Deleting a property means removing a key-value pair from an object.

Example:

let employee = {

    name: "John",

    department: "QA",

    experience: 5

};

delete employee.experience;

console.log(employee);

Output

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

The experience property has been completely removed from the object.


Why Do We Delete Properties?

Deleting properties helps developers:

  • Remove unnecessary data.

  • Clean objects before sending API requests.

  • Save memory by eliminating unused properties.

  • Create dynamic objects.

  • Maintain clean and organized data.

  • Modify application state.


Using the delete Operator

The delete operator removes a property from an object.

Syntax

delete objectName.propertyName;

Deleting a Property Using Dot Notation

let student = {

    name: "Alice",

    age: 22,

    course: "JavaScript"

};

delete student.age;

console.log(student);

Output

{ name: 'Alice', course: 'JavaScript' }

Deleting a Property Using Bracket Notation

Bracket notation can also be used with the delete operator.

let product = {

    name: "Laptop",

    price: 65000

};

delete product["price"];

console.log(product);

Output

{ name: 'Laptop' }

Deleting Properties Using Variables

Bracket notation is useful when the property name is stored in a variable.

let employee = {

    name: "David",

    city: "Delhi"

};

let property = "city";

delete employee[property];

console.log(employee);

Output

{ name: 'David' }

Checking a Deleted Property

After deletion, accessing the property returns undefined.

let car = {

    brand: "Toyota",

    model: "Camry"

};

delete car.model;

console.log(car.model);

Output

undefined

Deleting Nested Object Properties

Properties inside nested objects can also be deleted.

let employee = {

    name: "John",

    address: {

        city: "Bangalore",

        state: "Karnataka"

    }

};

delete employee.address.state;

console.log(employee);

Output

{
  name: 'John',
  address: { city: 'Bangalore' }
}

Real-World Example

Suppose a customer no longer has a membership.

let customer = {

    name: "Rahul",

    membership: "Gold"

};

delete customer.membership;

console.log(customer);

Output

{ name: 'Rahul' }

Another example:

Remove a discount property.

let product = {

    name: "Keyboard",

    discount: 20

};

delete product.discount;

console.log(product);

Output

{ name: 'Keyboard' }

Automation Testing Example

Automation engineers frequently delete properties while cleaning test data, preparing API payloads, and managing configuration objects.

Playwright Example

Remove a browser option.

const browserConfig = {

    browser: "chromium",

    headless: true

};

delete browserConfig.headless;

console.log(browserConfig);

Output

{ browser: 'chromium' }

Selenium Example

Remove browser capability.

const capabilities = {

    browserName: "chrome",

    version: "latest"

};

delete capabilities.version;

console.log(capabilities);

Output

{ browserName: 'chrome' }

Cypress Example

Remove login information.

const user = {

    username: "admin",

    password: "admin123"

};

delete user.password;

console.log(user);

Output

{ username: 'admin' }

API Testing Example

Remove an unnecessary field from a request payload.

const request = {

    method: "POST",

    endpoint: "/users",

    debug: true

};

delete request.debug;

console.log(request);

Output

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

Data-Driven Testing Example

Remove temporary test data.

const credentials = {

    username: "tester",

    password: "test123",

    temporary: true

};

delete credentials.temporary;

console.log(credentials);

Output

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

Common Mistakes

Setting a Property to null Instead of Deleting It

Incorrect:

employee.department = null;

The property still exists.

Result:

{ department: null }

Correct:

delete employee.department;

The property is completely removed.


Expecting delete to Remove Variables

Incorrect:

let age = 25;

delete age;

The delete operator removes object properties, not variables declared with let, const, or var.


Accessing a Deleted Property

delete employee.city;

console.log(employee.city);

Output

undefined

Always check whether a property exists before using it.


Best Practices

Delete Only Unnecessary Properties

Remove properties only when they are no longer needed.


Use Dot Notation for Known Properties

Dot notation is cleaner and easier to read.


Use Bracket Notation for Dynamic Property Names

If the property name is stored in a variable, bracket notation is the better choice.


Avoid Excessive Deletion

Frequent deletion of properties in performance-critical applications can affect optimization. Consider setting a property to null only when you intentionally want to keep the property but clear its value.


Conclusion

Deleting properties is an important JavaScript operation that allows developers to remove unnecessary key-value pairs from objects. The delete operator permanently removes a property, making the object cleaner and more efficient for the required task.

For automation engineers, deleting properties is especially useful when cleaning API payloads, managing configuration objects, removing temporary test data, and preparing dynamic test scenarios. Understanding the delete operator helps create flexible and maintainable automation scripts.


Frequently Asked Questions (FAQs)

What is the delete operator?

The delete operator removes a property from an object.


How do you delete a property?

delete objectName.propertyName;

Can I use bracket notation with delete?

Yes.

delete objectName["propertyName"];

What happens after a property is deleted?

The property is removed completely, and accessing it returns:

undefined

Does delete remove variables?

No. It only removes object properties.


Why is deleting properties important in automation testing?

Automation engineers delete properties to clean API request payloads, remove temporary test data, update configuration objects, and prepare dynamic test scenarios efficiently.


Key Takeaways

  • The delete operator removes properties from objects.

  • Deleted properties no longer exist in the object.

  • Accessing a deleted property returns undefined.

  • Both dot notation and bracket notation work with delete.

  • Nested object properties can also be deleted.

  • The delete operator removes object properties, not variables.

  • Deleting properties helps keep objects clean and organized.

  • It is widely used in JavaScript, APIs, and automation testing.

  • Use deletion only when the property is no longer needed.

  • Mastering the delete operator is essential for effective JavaScript object management.