DEV Community

Discussion on: How would you refactor this JS function?

Collapse
 
andi23rosca profile image
Andi Rosca

My version:
I like to leave the happy path as the default return, and early return for any
exceptions

const lineChecker = (line, isFirstLine) => {
  if (!line) return "<br />";
  if (!isFirstLine) return `<p>${line}</p>`;
  return `<h1>${line}</h1>`;
};
Enter fullscreen mode Exit fullscreen mode