By the end of this lesson
Use explicit conditions and understand common truthiness traps.
Before you start
- Node.js 24 with a terminal and plain-text editor; the browser-only lesson is labelled separately.
- Complete lesson 5 or be comfortable with its examples.
Three words to know
- Truthy
- A value treated as true in a boolean context.
- Falsy
- A value treated as false, such as 0 or an empty string.
- Branch
- A group of statements selected by a condition.
1. Read the first matching branch
if, else if and else select a path. Braces keep the body visible even when it contains one line. The first matching branch wins, so put specific cases before broader ones.
const count = 3;
if (count === 0) {
console.log("empty");
} else if (count < 5) {
console.log("small");
} else {
console.log("large");
}
What you should see · example output
small
2. Truthiness is not a data contract
A nonempty string such as "false" is truthy. Empty arrays and objects are also truthy. Check length or a specific field when that is the actual rule, rather than guessing from if(value).
console.log(Boolean("false"));
console.log(Boolean([]));
console.log(Boolean(""));
console.log(Boolean(0));
What you should see · example output
true true false false
3. Combine conditions
&& requires both expressions to be truthy; || chooses based on truthiness. These operators return an operand rather than always returning a boolean. For a simple validation rule, explicit comparisons make intent easier to review.
const pages = 10;
const allowed = Number.isInteger(pages) && pages >= 1 && pages <= 50;
console.log(allowed);
What you should see · example output
true
YOUR TURN
A small check before you continue
How would you test that an array has at least one item? Is if([]) sufficient?
Show a suggested answer
Use Array.isArray(value) && value.length > 0 when external input may not be an array. if([]) is true and therefore does not mean nonempty.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
