Skip to content

Instantly share code, notes, and snippets.

@cihat
Created November 16, 2020 11:12
Show Gist options
  • Select an option

  • Save cihat/4b6cb79def2df2fda52dd63a7d76bf0c to your computer and use it in GitHub Desktop.

Select an option

Save cihat/4b6cb79def2df2fda52dd63a7d76bf0c to your computer and use it in GitHub Desktop.
Removing Properties
Just as properties can be added to objects at any time, they can also be
removed. Simply setting a property to null doesn’t actually remove the
property completely from the object, though. Such an operation calls
[[Set]] with a value of null, which, as you saw earlier in the chapter, only
replaces the value of the property. You need to use the delete operator to
completely remove a property from an object.
The delete operator works on a single object property and calls an
internal operation named [[Delete]]. You can think of this operation as
removing a key/value pair from a hash table. When the delete operator is
successful, it returns true. (Some properties can’t be removed, and this is
discussed in more detail later in the chapter.) For example, the following
listing shows the delete operator at work:
var person1 = {
name: "Nicholas"
};
console.log("name" in person1); // true
delete person1.name; // true - not output
console.log("name" in person1); // false
console.log(person1.name); // undefined
In this example, the name property is deleted from person1. The
in operator returns false after the operation is complete. Also, note
that attempting to access a property that doesn’t exist will just return
undefined u. Figure 3-2 shows how delete affects an object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment