Clean Code #1 : React, Javascript

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.f…


This content originally appeared on DEV Community and was authored by Moni

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()


This content originally appeared on DEV Community and was authored by Moni


Print Share Comment Cite Upload Translate Updates
APA

Moni | Sciencx (2021-04-30T15:41:24+00:00) Clean Code #1 : React, Javascript. Retrieved from https://www.scien.cx/2021/04/30/clean-code-1-react-javascript/

MLA
" » Clean Code #1 : React, Javascript." Moni | Sciencx - Friday April 30, 2021, https://www.scien.cx/2021/04/30/clean-code-1-react-javascript/
HARVARD
Moni | Sciencx Friday April 30, 2021 » Clean Code #1 : React, Javascript., viewed ,<https://www.scien.cx/2021/04/30/clean-code-1-react-javascript/>
VANCOUVER
Moni | Sciencx - » Clean Code #1 : React, Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/30/clean-code-1-react-javascript/
CHICAGO
" » Clean Code #1 : React, Javascript." Moni | Sciencx - Accessed . https://www.scien.cx/2021/04/30/clean-code-1-react-javascript/
IEEE
" » Clean Code #1 : React, Javascript." Moni | Sciencx [Online]. Available: https://www.scien.cx/2021/04/30/clean-code-1-react-javascript/. [Accessed: ]
rf:citation
» Clean Code #1 : React, Javascript | Moni | Sciencx | https://www.scien.cx/2021/04/30/clean-code-1-react-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.