I guess there are different ways to do that, and all of them are valid. I prefer to use an if-statement:
if (email) {
user.email = email;
}
You could also use a setter (which you might even make very generic, so you could assign it to any object):
const user = {
email: undefined,
getEmail: () => this.email,
setEmail: email => { this.email = email || this.email; },
unsetEmail: () => { this.email = undefined; },
};
user.setEmail(email);
If you want to be extra careful (since above solutions might be dangerous in certain situations):
if (typeof email !== 'undefined') {
user.email = email;
}
// alternatively, using roption-js or similar,
// which adds Rust inspired Options
if (email.isSome()) {
user.email = email.unwrap();
}