Loading episodes…
0:00 0:00

How do I remove a property from a JavaScript object?

00:00
BACK TO HOME

How do I remove a property from a JavaScript object?

10xTeam January 01, 2023 2 min read

To remove a property from a JavaScript object, you can use the delete operator. For example:

let obj = {
  foo: 'bar',
  baz: 42
};

delete obj.foo;

console.log(obj); // { baz: 42 }

This will remove the foo property from the obj object.

Alternatively, you can also use the Object.defineProperty() method to change the value of a property’s configurable attribute to true, and then use the delete operator to remove the property. For example:

let obj = {
  foo: 'bar',
  baz: 42
};

Object.defineProperty(obj, 'foo', { configurable: true });
delete obj.foo;

console.log(obj); // { baz: 42 }

Keep in mind that the delete operator only works on own properties (properties that are directly present on an object, not inherited from its prototype). It cannot delete inherited properties or properties that have the configurable attribute set to false.


Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Audio Interrupted

We lost the audio stream. Retry with shorter sentences?