Simple before insert question

I am getting data from a API and before is is inserted into my collection I am combining items like:
name1= name1 + name2 + name3 + name4
I have this working, but say for example name2 and name4 is empty how do I not include those items in the concatenated string?

Any help would be appreciated.

just check if these variables have a value.
like this:

let fullName;

if(name1) // condition to check if this variable is defined and has a value.
fullName = fullName + name1; // add this value to ‘fullName’

if(name2)
fullName = fullName + name2;

if(name3)
fullName = fullName + name3;