0 day streak
javascript / beginner
Snippet

Logical OR (||) for Default Content

You can use the logical OR operator (||) in JSX to provide a fallback value. If the first value is falsy (like null or undefined), the second value will be displayed instead.

snippet.js
javascript
1
2
3
function Welcome({ username }) {
return <h1>Welcome, {username || 'Guest'}!</h1>;
}
react
Breakdown
1
{username || 'Guest'}
Checks if username exists; if not, it defaults to the string 'Guest'.