Example
lets say we have a object like below
const user = {
firstName: 'Vijay',
lastName: 'Singh'
}
Note : firstName and lastName can be empty string or undefined or null.
const fullName = user.firstName
? ( user.lastName ? `${user.firstName} ${user.lastName}`: user.firstName)
: user.lastName
Using || Operator
const fullName = `${user.firstName || '' } ${user.lastName || ''}`.trim()
Top comments (1)
This one is good too 👇