Search code examples
javascriptconsole.logarrow-functions

Add console log in an one arrow (auto return) function without adding curly braces


So consider you have a one line auto return arrow function:

const test = (val) => val;

How would you check the val without doing:

const test = (val) => {
  console.log(val);
  return val;
};

Solution

  • You can actually use || between the arrow function and the returned value like so:

    const test = (val) => console.log(val) || val;
    

    This will not interfere with the process and will also log val without the hassle of adding {} and return and undoing it once you're done