The 'DELETE' keyword in JavaScript
delete is an operator that removes a property from an object. After it runs, the object no longer has that property at all, so reading it gives undefined and loops over the object’s keys no longer see it. It returns true when the property is gone, which includes the case where the property never existed.
Removing a property
Section titled “Removing a property”const user = { name: 'Sam', email: 'sam@example.com', tempToken: 'x91f' };
console.log(delete user.tempToken);console.log(user);console.log(user.tempToken);console.log(delete user.nickname);This prints:
true{ name: 'Sam', email: 'sam@example.com' }undefinedtrueBracket notation works too, as in delete user['tempToken'], which allows the name to come from a variable.
Deleting is not the same as setting to undefined
Section titled “Deleting is not the same as setting to undefined”Setting a property to undefined leaves the property in place with an empty value. Deleting it removes the key, and the difference shows up in the in operator, in Object.keys() and in JSON.stringify():
const a = { name: 'Sam', phone: '01632 960000' };const b = { name: 'Sam', phone: '01632 960000' };
a.phone = undefined;delete b.phone;
console.log(a, 'phone' in a, Object.keys(a));console.log(b, 'phone' in b, Object.keys(b));This prints:
{ name: 'Sam', phone: undefined } true [ 'name', 'phone' ]{ name: 'Sam' } false [ 'name' ]What delete cannot remove
Section titled “What delete cannot remove”delete only removes the object’s own properties. An inherited property is not touched, because it belongs to the prototype. A property marked as not configurable cannot be deleted either: in sloppy mode delete returns false and leaves it, while in strict mode it throws a TypeError:
const settings = {};Object.defineProperty(settings, 'version', { value: 3, configurable: false });
console.log(delete settings.version);console.log(settings.version);
(function () { 'use strict'; try { delete settings.version; } catch (err) { console.log(err.message); }})();This prints:
false3Cannot delete property 'version' of #<Object>delete does not remove variables. Applied to a variable declared with var, let or const, it returns false in sloppy mode and leaves the variable alone, and in strict mode it is a syntax error.
Arrays
Section titled “Arrays”Using delete on an array element removes the value but not the slot, so the array keeps its length and is left with a hole. Use splice() to remove an element and close the gap:
const queue = ['tea', 'milk', 'bread'];delete queue[1];console.log(queue, queue.length);
const list = ['tea', 'milk', 'bread'];list.splice(1, 1);console.log(list, list.length);This prints:
[ 'tea', <1 empty item>, 'bread' ] 3[ 'tea', 'bread' ] 2Leaving the original alone
Section titled “Leaving the original alone”delete changes the object it is given. Where the original should stay as it is, for example before sending a record somewhere without a private field, object destructuring with a rest property builds a copy without that field:
const user = { name: 'Sam', email: 'sam@example.com', tempToken: 'x91f' };const { tempToken, ...safe } = user;console.log(safe);console.log(user);This prints:
{ name: 'Sam', email: 'sam@example.com' }{ name: 'Sam', email: 'sam@example.com', tempToken: 'x91f' }Steve Griffith covers this topic in his video delete Keyword in JavaScript.