What's the best way to delete an object property in JavaScript?

I have a JavaScript object and I need to get rid of one of its properties. Let me show you what I’m working with:

let userProfile = {
  "username": "johndoe",
  "email": "[email protected]",
  "tempToken": "abc123xyz"
};

I want to completely remove the tempToken property so my object looks like this:

let userProfile = {
  "username": "johndoe",
  "email": "[email protected]"
};

What are the different ways I can accomplish this in JavaScript? I’m looking for the most reliable method that works across different browsers.

Yeah, delete is the standard way to go, but there are a couple things to keep in mind. delete userProfile.tempToken works great for regular properties, but it can slow things down if you’re working with objects that need to keep their “shape” for JS engine optimizations. You could also use destructuring if you want a new object without that property: const {tempToken, ...cleanProfile} = userProfile. This keeps your original object untouched and gives you a clean copy. For what you’re doing though, just use the delete operator - it’s straightforward and has solid browser support.

u can just use delete userProfile.tempToken. super simple and works on all browsers, no fuss!

The delete operator is your best bet here. I’ve used delete userProfile.tempToken for years - works perfectly across all browsers. Quick tip the others missed: delete returns a boolean. You’ll get true if the property was deleted or didn’t exist, false if it couldn’t be deleted (non-configurable properties). With regular object properties like yours, it returns true and wipes the property from memory. Just remember delete won’t work on variables declared with var, let, or const - only object properties. For what you’re doing, delete userProfile.tempToken is exactly right.