javascript / beginner
Snippet
Fallback Values with Logical OR
The logical OR operator (||) can be used to set a fallback value. If the first value is null, undefined, or empty, JavaScript will use the string 'Anonymous' instead.
snippet.js
1
const userName = response.name || 'Anonymous';
nextjs
Breakdown
1
response.name
The preferred value we want to use.
2
||
The operator that checks if the left side is 'falsy'.
3
'Anonymous'
The default value provided as a safety net.