How to Clear an Object in JavaScript

avatar
Borislav Hadzhiev

Last updated: Dec 22, 2022
3 min

banner

# Table of Contents

  1. Clear an Object in JavaScript
  2. Only clearing a nested object
  3. Clear an Object by reassigning its variable in JavaScript
  4. Clearing an object using getOwnPropertyNames
  5. Clear an Object using Object.keys() and forEach()
  6. Clear an Object using Object.keys() and for...of

# Clear an Object in JavaScript

Use a for..in loop to clear an object and delete all its properties. The loop will iterate over all the enumerable properties in the object.

On each iteration, use the delete operator to delete the current property.

index.js
const obj = {a: 'one', b: 'two'}; for (const key in obj) { delete obj[key]; } console.log(obj); // ๐Ÿ‘‰๏ธ {}

clear object deleting all properties

The for...in loop iterates over all of an object's enumerable properties.

An enumerable property is one that is added to the object using simple assignment, e.g. dot . or square bracket [] notation.

Properties assigned to the object using the Object.defineProperty() method are non-enumerable by default and are not iterated over in a for...in loop.

The Object.defineProperty method is mostly used by third-party library creators and not in day-to-day code, so chances are the for...in loop covers all of your object's properties.

# Only clearing a nested object

The same approach can be used if you only need to clear a nested object.

index.js
const obj = { a: 'one', b: 'two', address: {c: 'three', d: 'four'}, }; for (const key in obj.address) { delete obj.address[key]; } console.log(obj); // ๐Ÿ‘‰๏ธ { a: 'one', b: 'two', address: {} }

only clearing nested object

We used a for...in loop to iterate over the obj.address nested object.

On each iteration, we delete the current key-value pair.

If you need to clear an entire object, it would be much more performant to reassign the variable.

# Clear an Object by reassigning its variable in JavaScript

This is a two-step process:

  1. Declare the variable that stores the object using the let keyword.
  2. Reassign the variable to an empty object.
index.js
let obj = {a: 'one', b: 'two'}; obj = {}; console.log(obj); // ๐Ÿ‘‰๏ธ {}

clear object by reassigning its variable

It is much more performant to reassign the variable that stores the object, setting it to an empty object.

This is only possible if you used the let or var keywords to declare the variable.

Notice that we used the let keyword when declaring the obj variable.

The let keyword allows us to reassign the variable. Variables declared using const cannot be reassigned.

# Clearing an object using getOwnPropertyNames

In the extremely unlikely scenario that your object contains non-enumerable properties, you can use the following approach to clear an object.

index.js
let obj = {a: 'one', b: 'two'}; Object.defineProperty(obj, 'color', { value: 'red', enumerable: false, // ๐Ÿ‘ˆ๏ธ defaults to false configurable: true, writable: true, }); const allProperties = Object.getOwnPropertyNames(obj); console.log(allProperties); // ๐Ÿ‘‰๏ธ ['a', 'b', 'color'] allProperties.forEach(property => { delete obj[property]; }); console.log(Object.getOwnPropertyNames(obj)); // []

clearing object using getownpropertynames

We used the Object.getOwnPropertyNames() method to get an array of the enumerable and non-enumerable properties of the object.

We used the Array.forEach method to iterate over the array and cleared each property using the delete operator.

As previously mentioned, it's best to use the let keyword and reassign the variable to an empty object. This is the fastest way to clear the object and let the garbage collector do its job.

Another simple way to clear an object is to use the Object.keys() and Array.forEach() methods.

# Clear an Object using Object.keys() and forEach()

This is a three-step process:

  1. Use the Object.keys() method to get an array of the object's keys.
  2. Use the Array.forEach() method to iterate over the array of keys.
  3. Use the delete operator to delete each key from the object.
index.js
const obj = {a: 'one', b: 'two', c: 'three'}; Object.keys(obj).forEach(key => { delete obj[key]; }); console.log(obj); // ๐Ÿ‘‰๏ธ {}

The Object.keys() method returns an array of the object's keys.

index.js
const obj = {a: 'one', b: 'two', c: 'three'}; // ๐Ÿ‘‡๏ธ [ 'a', 'b', 'c' ] console.log(Object.keys(obj));

The function we passed to the Array.forEach() method gets called with each element of the array.

index.js
const obj = {a: 'one', b: 'two', c: 'three'}; Object.keys(obj).forEach(key => { delete obj[key]; }); console.log(obj); // ๐Ÿ‘‰๏ธ {}

On each iteration, we use the delete statement to delete the current key.

After the last iteration, the object is empty.

# Clear an Object using Object.keys() and for...of

The same approach can be used to clear an object using a for...of loop.

index.js
const obj = {a: 'one', b: 'two', c: 'three'}; for (const key of Object.keys(obj)) { delete obj[key]; } console.log(obj); // ๐Ÿ‘‰๏ธ {}

We used the Object.keys() method to get an array of the object's keys.

The next step is to use a for...of loop to iterate over the array, removing each key.

The for...of statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev