js remove from object by key

Delete Key from JavaScript Object

By William Walker
AD

In JavaScript, objects are key-value pairs stored in curly braces {}. Sometimes, you may need to remove a specific key and its associated value from an object. This article will guide you through various ways to achieve this, ensuring your code is efficient and performant.

Before we delve into the details, let's first understand why you might want to remove a key from an object. You could be cleaning up redundant or unnecessary data, enhancing security by removing sensitive information, or optimizing memory usage by discarding unused data.

Removing a Key Using delete Operator

The delete operator is the most straightforward method to remove a key-value pair from an object.

AD
Remove last element from an array in JavaScript
Remove last element from an array in JavaScript
What Is Closure In Javascript, Javascript Closure Example, How To Use Closures In Javascript, Javascript Closure Explanation, Javascript Practice Questions, Choosing Between Java And Javascript, Javascript Programming Concepts, Recursive Javascript Function, Javascript Apply Call Bind Difference
What Is Closure In Javascript, Javascript Closure Example, How To Use Closures In Javascript, Javascript Closure Explanation, Javascript Practice Questions, Choosing Between Java And Javascript, Javascript Programming Concepts, Recursive Javascript Function, Javascript Apply Call Bind Difference
javascript Interview preparing 2026 #programming #codingresources #coding Interview, Coding
javascript Interview preparing 2026 #programming #codingresources #coding Interview, Coding

Here's the syntax:

delete obj.property

How to return object literals from arrow functions in JavaScript
How to return object literals from arrow functions in JavaScript
web development programming coding
web development programming coding
an image of a computer screen with the text'document object model'on it
an image of a computer screen with the text'document object model'on it

or, if you have the property name stored in a variable

delete obj[propertyName]

AD

Using delete on Existing Properties

Let's create an object, add some properties, and then use the delete operator to remove one of them.

Remove duplicate from an array using JavaScript #programming #webdevelopment #jsinterview #reelstrending Web Development
Remove duplicate from an array using JavaScript #programming #webdevelopment #jsinterview #reelstrending Web Development
the instructions for how to use an object in visual language, including text and numbers
the instructions for how to use an object in visual language, including text and numbers
🚀 Day 3/365 – JavaScript Object Methods Still confused about JavaScript Objects? 🤔 If you're serious about becoming a JavaScript developer, mastering Object Methods is a must. These methods help you manage, inspect, clone, and optimize objects in real-world applications. 📌 In today's cheat sheet you'll learn: ✅ Object.keys() ✅ Object.values() ✅ Object.entries() ✅ Object.assign() ✅ Object.freeze() ✅ Object.seal() ✅ hasOwnProperty() ✅ Object.prototype.hasOwnProperty.call() ✅ Object.defineP... Cheat Sheets, How To Become
🚀 Day 3/365 – JavaScript Object Methods Still confused about JavaScript Objects? 🤔 If you're serious about becoming a JavaScript developer, mastering Object Methods is a must. These methods help you manage, inspect, clone, and optimize objects in real-world applications. 📌 In today's cheat sheet you'll learn: ✅ Object.keys() ✅ Object.values() ✅ Object.entries() ✅ Object.assign() ✅ Object.freeze() ✅ Object.seal() ✅ hasOwnProperty() ✅ Object.prototype.hasOwnProperty.call() ✅ Object.defineP... Cheat Sheets, How To Become
js remove from object by key
js remove from object by key
JavaScript Object Methods Cheat Sheet
JavaScript Object Methods Cheat Sheet
Check if object is empty in Javascript
Check if object is empty in Javascript

let obj = { name: "John", age: 30, job: "Engineer" }; delete obj.job; console.log(obj); // Output: { name: "John", age: 30 }

Using delete on Non-Existing Properties

Attempting to delete a non-existing property will have no effect but won't throw an error. However, using delete on a non-configurable property (like those defined with Object.defineProperty) will throw a TypeError.

AD

let obj = { name: "John" }; delete obj.job; // No effect as 'job' is not a property of obj

JavaScript Object{객체}
JavaScript Object{객체}
js remove from object by key
js remove from object by key
Learn JS
Learn JS
Object Destructuring In Javascript
Object Destructuring In Javascript
Javascript Programming Wallpaper, Recursive Javascript Function, Javascript Wallpaper, Javascript Code Wallpaper, Javascript Coding Concepts, Javascript Functional Programming Tutorial, Functional Programming In Javascript, Javascript Coding Graphic, Javascript Code Screenshot
Javascript Programming Wallpaper, Recursive Javascript Function, Javascript Wallpaper, Javascript Code Wallpaper, Javascript Coding Concepts, Javascript Functional Programming Tutorial, Functional Programming In Javascript, Javascript Coding Graphic, Javascript Code Screenshot
Common Express.js Mistakes Beginners Make — Don't Do This vs Do This
Common Express.js Mistakes Beginners Make — Don't Do This vs Do This
Using switch Statements in JavaScript
Using switch Statements in JavaScript
Iterate over an object
Iterate over an object
How to Create Objects in JavaScript?
How to Create Objects in JavaScript?
a bunch of ice cubes sitting next to a key on top of each other
a bunch of ice cubes sitting next to a key on top of each other
Javascript Objects | Beginner to Advance Javascript Tutorials | Lesson 08
Javascript Objects | Beginner to Advance Javascript Tutorials | Lesson 08
the most confusing word in javescript this is an image of a man pointing at something
the most confusing word in javescript this is an image of a man pointing at something
JS Types
JS Types

Removing a Key Using Object rugged

Another way to remove a key-value pair is by creating a new object with all keys except the one you want to remove. This can be achieved using the spread operator ({ ... }).

Creating a New Object Without Specific Keys

Let's see how to use the spread operator to achieve this:

AD

let obj = { name: "John", age: 30, job: "Engineer" }; let newObj = { ...obj }; delete newObj.job; console.log(newObj); // Output: { name: "John", age: 30 }

Removing Multiple Keys

The spread operator can also be used to remove multiple keys at once. Just ignore the keys you want to remove while spreading:

let obj = { name: "John", age: 30, job: "Engineer", country: "USA" }; let newObj = { ...obj, job: undefined, country: undefined }; console.log(newObj); // Output: { name: "John", age: 30 }

Removing keys from an object is a common operation in JavaScript. Understanding how to do it effectively can greatly improve the efficiency and maintainability of your code. By mastering the delete operator and the spread operator ({ ... }), you've got a powerful toolset at your disposal.

AD