Debug Arrow Functions With Comma Operator

Use the Comma Operator to execute many JavaScript expressions in one ‘location’, but only return the value from the last expression.

This is commonly used in classic for loops, but has an interesting use case to quickly debug a single-line arrow function. For example, suppose you wanted to throw a console.log() into this function:

const myFunc = value => doCrazyOperation(value.foo);

Without the comma operator, this function would have to be expanded into a multi-line “block” function with a return statement, which is relatively quite longer:

const myFunc = (value) {
  console.log("value", value);
  return doCrazyOperation(value.foo);
};

With the Comma Operator, this debugging is much easier to write and maintain:

const myFunc = value => (console.log("value", value), doCrazyOperation(value.foo));
Written on January 19, 2018 by evanbrodie