DEV Community

Discussion on: How would you refactor this JS function?

Collapse
 
wrthfl profile image
wrthfl

My, maybe naive solution would be this:

const checkLines = (line = "", isFirstline = false) => {
  if (typeof line !== "string") return false;
  if (line === "") return "<br />";
  if (isFirstline) return `<h1>${line}</h1>`;
  else return `<p>${line}</p>`;
}
Enter fullscreen mode Exit fullscreen mode

Not a fan of ternary operators, so i put it like this.
Would love to get feedback on this too since i am most certainly not a pro.

Collapse
 
rkallan profile image
RRKallan

Assumption when line is not a string return empty string or nothing. In that case there would be no need for a check to print the line. But as I said, it’s a assumption.

The else is redundant and could be replaced with the return statement.