← Back to JavaScript, page 8

javascript-0158 · Easy fresher

What do the comma operator, void, and delete do, and when do they matter?

The comma operator evaluates several expressions and returns the last one. void evaluates an expression and always returns undefined. delete removes a property from an object and returns true unless the property cannot be removed. They are rarely needed, but they appear in minified code, for loops, arrow functions that must return nothing, and in questions about arrays and variables.

Full answer

Comma operator: (a, b, c) evaluates a, then b, then c, and the value of the whole expression is c. It is different from the commas in argument lists, array literals, and let a = 1, b = 2, which only separate items. Practical uses: two updates in a for loop header, for (let i = 0, j = 10; i < j; i++, j--), and compact code written by minifiers. Avoid it in normal code, because it hides what is returned.

void expression: evaluates the expression and returns undefined. Uses:

  • void 0 is a safe way to get undefined in old code, where undefined could be overwritten in some scopes.
  • An arrow function that should return nothing: () => void doSomething(). This stops a callback from accidentally returning a value.
  • Marking a promise as intentionally not awaited: void fetchData(), which also satisfies lint rules against floating promises.
  • Legacy: <a href="javascript:void(0)"> to stop navigation. Use a button instead.

delete object.property: removes an own property and returns true if the property is gone or never existed, and false for a non-configurable property (in strict mode it throws a TypeError). Important points:

  • It removes only the property, not the object it referenced (garbage collection frees that when no references remain).
  • It does not affect inherited properties: deleting an own property that shadows a prototype property reveals the inherited one again.
  • On arrays, delete arr[1] leaves a hole and keeps length. Use splice to remove elements.
  • It cannot delete variables declared with var, let, or const, nor functions.
  • Deleting properties on hot objects can slow down engines, so for dictionaries, prefer Map, or set the value to undefined or null.
  • delete on a frozen or sealed object fails.

A related point: delete obj.x is different from obj.x = undefined. After the first, 'x' in obj is false. After the second, it is true.

javascript
let a = (1, 2, 3);
console.log(a, (console.log('evaluated first'), 'result'));
for (let i = 0, j = 5; i < j; i++, j--) console.log('i', i, 'j', j);

console.log(void 0, void 'text', void (1 + 2), typeof void 0);
const handler = () => void console.log('side effect only');
console.log('arrow with void returns:', handler());
const noisy = () => 42;
console.log([1].map(() => void noisy()), [1].map(() => noisy()));

const user = { name: 'Asha', age: 30, nickname: undefined };
console.log(delete user.age, user, 'age' in user, Object.keys(user));
console.log('nickname is undefined but still a key:', 'nickname' in user);
console.log(delete user.missing, delete user.name, user);

const proto = { kind: 'prototype value' };
const child = Object.create(proto);
child.kind = 'own value';
console.log(child.kind);
delete child.kind;
console.log('after delete, inherited value shows:', child.kind);

const list = [1, 2, 3];
delete list[1];
console.log(list, list.length, 1 in list);
list.splice(1, 1);
console.log(list);

const frozen = Object.freeze({ x: 1 });
console.log('delete on frozen object (sloppy mode):', delete frozen.x, frozen);
(function () {
  'use strict';
  try {
    delete frozen.x;
  } catch (error) {
    console.log('strict mode:', error.name);
  }
})();

var declared = 1;
console.log('delete a var declaration:', delete globalThis.declared, typeof declared);

Run with Node.js v24.22.1 as js-158.js; two runs gave identical output. Output: evaluated first 3 result i 0 j 5 i 1 j 4 i 2 j 3 undefined undefined undefined undefined side effect only arrow with void returns: undefined [ undefined ] [ 42 ] true { name: 'Asha', nickname: undefined } false [ 'name', 'nickname' ] nickname is undefined but still a key: true true true { nickname: undefined } own value after delete, inherited value shows: prototype value [ 1, <1 empty item>, 3 ] 3 false [ 1, 3 ] delete on frozen object (sloppy mode): false { x: 1 } strict mode: TypeError delete a var declaration: true number

Key points

  • Comma operator evaluates all operands and returns the last; common in for loop headers and minified code.
  • void returns undefined; used in void 0, no-return arrows, and intentionally floating promises.
  • delete removes an own, configurable property and returns true or false (throws in strict mode when it fails).
  • delete on arrays leaves holes; use splice. It does not remove variables or inherited properties.
  • delete differs from setting undefined: the key disappears from in and Object.keys.

Common mistakes

  • Using delete on array elements and expecting the length to shrink.
  • Using delete on variables.
  • Setting a property to undefined and expecting it to disappear from Object.keys.
  • Writing comma expressions that hide the returned value.

Follow-ups

  • Why can delete slow down an object in an engine?
  • What is the difference between delete obj.x and obj.x = undefined?
  • Where does void appear in lint rules about promises?

Tips

  • Describe each operator in one sentence and give one real use.
  • Mention the array hole behavior of delete.
  • Note that none of these are needed often.