# JavaScript truthiness explorer

> Compare Boolean(value), logical OR and nullish coalescing for zero, empty strings, null, undefined, arrays and objects.

- Canonical HTML: [https://unrust.dev/tools/javascript-truthiness](https://unrust.dev/tools/javascript-truthiness)
- Markdown representation: [https://unrust.dev/tools/javascript-truthiness/index.md](https://unrust.dev/tools/javascript-truthiness/index.md)

## How to use the tool

Pick one of the built-in JavaScript values and compare all three results. The fallback is the string "fallback". This explorer demonstrates truthiness, not loose equality or input validation.

## Worked example

Logical OR (||) chooses its right operand when the left value is falsy. Nullish coalescing (??) chooses it only for null or undefined. With a left value of 0, || returns the fallback and ?? keeps 0.

```text
const retries = 0;
retries || "fallback"; // "fallback"
retries ?? "fallback"; // 0
```

Inputs stay in your browser. These are open learning examples, not scored diagnostic drills.

[Keep zero: choosing between || and ?? in JavaScript](https://unrust.dev/blog/javascript-defaults-or-vs-nullish/index.md)

[MDN: nullish coalescing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing)

## Practice choosing the right default

An empty label is allowed. Should label || "Untitled" and label ?? "Untitled" be treated as interchangeable?

No. An empty string is falsy, so || replaces it. ?? preserves it and substitutes only for null or undefined. Decide whether an empty label is allowed before choosing the operator.

Original open example, not a scored drill. Close tools before an unassisted session.

[Practice checking the intended behavior](https://unrust.dev/guides/reading-code-against-a-contract/index.md)

[Take the free diagnostic](https://unrust.dev/diagnostic/index.md)
