While it may not be crucial in this situation, you might want to consider utilizing the nullish coalescing operator (??
) instead of ||
. This operator only returns the value on the right if the value on the left is specifically null
or undefined
, rather than any falsy value like ''
.
You can approach this in two different ways:
// Using your original method
// Keep in mind that + has higher precedence than ?? and ||, so parentheses are necessary here
a = (a ?? '') + (b ?? '')
// Set a default value for a first
a ??= ''
// Then add b or '' if b is null to a
a += b ?? ''
The ??=
syntax belongs to TypeScript's short-circuiting assignment operators, where a ??= ''
is equivalent to a = a ?? ''
.
Nevertheless, none of these approaches are as concise as simply using a += b
.