πŸŽƒ Spookyberb πŸŽƒ – 13-03 Nov 23

Will this piece of code crash the app if it doesnt find anything?
const { fullName } = clients.find((client) => client.clientId === customerId);
const { fullName } = clients.find((client) => client.clientId === customerId);
UU
Unknown Userβ€’512d ago
S
ScriptyChrisβ€’512d ago
@β›„Snowberbβ›„ add a default value if .find() returns null:
const { fullName } = clients.find((client) => client.clientId === customerId) ?? { fullName: '' };
const { fullName } = clients.find((client) => client.clientId === customerId) ?? { fullName: '' };
S
Sniberbβ€’512d ago
ooo I like this that's what I wanted but why ?? and not ||?
UU
Unknown Userβ€’512d ago
S
ScriptyChrisβ€’512d ago
Alternatively you could use optional chaining without spread πŸ€”
const fullName = clients.find((client) => client.clientId === customerId)?.fullName;
const fullName = clients.find((client) => client.clientId === customerId)?.fullName;
!mdn optional chaining
UU
Unknown Userβ€’512d ago