JS: Reflect.deleteProperty

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2015)

Reflect.deleteProperty(obj, key)

Deletes the property key from obj.

Return true if

  • key exist and is deleted
  • key does not exist

else false.

🟢 tip: This function is similar to the delete (operator), but in a function form. [see JS: Function Form vs Operator] Also, it also has more sensible return value.

const jj = { dog: 1 };
Reflect.deleteProperty(jj, "dog");
console.assert(
 Object.hasOwn(jj, "dog") === false,
);
/* example when key doesn't exist */
const jj = { dog: 1 };
console.assert(
 Reflect.deleteProperty(jj, "y"),
);
/* example when deleting failed */
const jj = { dog: 1 };
Object.freeze(jj);
console.assert(
 Reflect.deleteProperty(jj, "dog") === false,
);