π¨π»βπ» π 3 Ways to Remove a Property from a JavaScript Object βοΈ
In JavaScript, objects are like containers holding key-value pairs π. But what if you need to remove a property β from an object?
2 min read 1 day ago
Letβs explore three approaches to accomplish this:
Not a Member Click here to read free
1. The delete
Keyword ποΈ
This is the most straightforward method. Hereβs how it works:
let obj = {
name: "Alice",
age: 30,
city: "New York"
};
// Delete the 'age' property
delete obj.age;
console.log(obj); // Output: { name: "Alice", city: "New York" }
In this example, delete obj.age;
removes the age
property from the obj
object. The delete
operator returns true
if the deletion is successful and false
if the property doesn't exist or can't be deleted (like non-configurable properties) π€.
Important Note: Built-in prototype properties are often non-configurable, so you canβt use delete
on them.