0 day streak
javascript / beginner
Snippet

Logical OR (||) for Default Values

The logical OR operator (||) returns the first 'truthy' value it finds. It is commonly used to assign a default value if the primary value is null or undefined.

snippet.js
javascript
1
2
3
const input = null;
const username = input || "Guest";
console.log(username); // "Guest"
nodejs
Breakdown
1
input || "Guest"
Since input is null (falsy), the operator moves to the second operand 'Guest'.