DEV Community

Discussion on: How would you refactor this JS function?

Collapse
 
baenencalin profile image
Calin Baenen

Here's a good one:

const lineChecker = (line, isLn1) => {
    isLn1 = isLn1 ?? false; line = line ?? "";
    const nEmpty = line.length > 0;
    let doc = "";

    if(nEmpty && isLn1) doc += `<h1>${line}</h1>`;
    if(nEmpty && !isLn1) doc += `<p>${line}</p>`;
    if(!nEmpty) doc += "<br/>";

    return doc;
};
Enter fullscreen mode Exit fullscreen mode