DEV Community

Discussion on: How would you refactor this JS function?

Collapse
eecolor profile image
EECOLOR

I don't like boolean parameters. Removing the boolean by splitting it into two functions makes is a lot more readable. At the call site you will know if it is the first line or not.

function formatFirstLine(line) {
  return line && `<h1>${line}</h1>`
}

function formatLine(line) {
  return line
    ? `<p>${line}</p>`
    : `<br />`
}
Enter fullscreen mode Exit fullscreen mode

Please note that formatFirstLine assumes line is a string.