5

Suppose you have an instance of an object, obj, and want to unset set a property called prop. What are the ultimate differences with the following two variants:

delete obj.prop;
obj.prop = undefined;

I believe that these both have the same meaning, but in the case of setting the property to undefined, the reference to the property does not have to be removed so a little work is saved. Correct me if I'm wrong.

  • 3
    *both have the same meaning* - Nope. In the first case, `prop` is **NOT** a property of `obj`, but in the second case, `prop` is a property of `obj` with value `undefined`. – thefourtheye Aug 27 '15 at 19:21
  • 2
    Typing the title of your question into Google brought me the duplicate. Maybe try that first. –  Aug 27 '15 at 19:23
  • 1
    @user1106925 typing my question into google [brought me to this question](https://i.imgur.com/QexoNDo.png). It's very annoying when the first Google search result is someone from 7 years ago telling me to use Google. That's what I'm doing. – Boris Verkhovskiy Mar 30 '22 at 20:31
  • Same here.. In the end I found this post that could help: https://stackoverflow.com/questions/14967535/delete-a-x-vs-a-x-undefined – Sanlok Lee Oct 07 '22 at 17:55

0 Answers0