Null-checking for specific field values

I also have a null-checking question that might be similar to this. I want to show the profile.middleName field if it exists for someone, but remove it if the field is empty. I tried to add the field to the name in the “title:” like the first and last names but if it doesn’t exist it appears as “undefined”. How do I resolve this?

Screen Shot 2021-02-25 at 2.21.46 PM Screen Shot 2021-02-25 at 2.31.36 PM

EDIT: I was able to fix this using our regular null-checking training here, and made it so that if there is content in the middle name field ? show firstName middleName lastName : but if there’s none, show firstName lastName. Here’s what I used:

title: profile.middleName ? `${profile.firstName} ${profile.middleName} ${profile.lastName}` : `${profile.firstName} ${profile.lastName}`,
1 Like