I have these columns for first name, preferred name, and last name that all flows into Full Name where it is formatted as:
FirstName (PreferredName) LastName

However, sometimes someone may not have a preferred name. Is there a way to not include the parentheses if the preferred name is blank?
This is my template currently:

Create a separate template column that adds the parentheses to the Preferred Name.
Then create an if-then-else column as follows:
- if Preferred Name is not empty, then template column
And then use that if-then-else column in your final template
Just tried to achieve this with a one column using JS and here’s what I got! 

function getFullName(firstName, preferredName = "", lastName) {
let fullName = "";
if (preferredName === "") {
fullName = lastName !== "" ? `${firstName} ${lastName}` : firstName;
} else {
fullName = lastName !== "" ? `${firstName} ${preferredName} ${lastName}` : `${firstName} ${preferredName}`;
}
return fullName;
}
const result = getFullName(p1, p2, p3);
const parts = result.split(' ').filter(part => part !== 'undefined');
const cleanedResult = parts.length === 1 ? parts[0] : parts.join(' ');
return cleanedResult;
Thank you
Here’s a shorter version.
const getFullName = (firstName, preferredName = '', lastName) =>
(firstName || '') + (preferredName ? ` (${preferredName})` : '') + (lastName ? ` ${lastName}` : '');
return getFullName(p1, p2, p3);
Using ternary and logical OR operators can simplify it.
2 Likes
Awesome Thinh! Thanks for sharing this! Still I’m learning JS in school and I hope one day I can also short my long codes 

1 Like
system
Closed
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.