Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 06 / 31FLOW AND SCOPE

Truth and short circuits.

Conditions use truthiness, and logical operators can return non-booleans.

01 / THE BASICS

Start here.

An if statement needs a yes-or-no decision. JavaScript can turn many values into that decision without requiring the literal words true or false. This conversion is called truthiness. Zero, the empty string, null, undefined, NaN, and false count as false. Other values count as true, including an empty array [] and an empty object {}. An empty collection still exists, so its truthiness does not tell you whether it contains elements.

The && operator reads as 'and'. It first evaluates the left side. If that side is falsy, the result is already determined and JavaScript returns that left value without evaluating the right. Otherwise it evaluates and returns the right side. The || operator reads as 'or': it returns the first truthy operand, or the last operand if none is truthy. These operators can therefore return a number or string, not only a boolean.

For example, name && name.trim() calls the string's trim method only when name is truthy. If name is the empty string, the result is that empty string and trim is never called. This can be concise, but a hidden side effect on the right can be hard to notice. For important actions, an if block makes the condition and action easier to read. To ask whether an array contains items, compare its length with zero rather than relying on truthiness.

02 / THE IDEA

Build the model.

A condition asks whether a value is truthy or falsy. False, zero, the empty string, null, undefined, and NaN are falsy; most other values, including empty arrays and objects, are truthy. This is a conversion to a decision, not a test of emptiness.

The && and || operators stop as soon as the answer is determined, returning one of their operands. That makes them useful for small expressions but can hide work if the right side has a side effect. Keep important actions in explicit branches.

03 / UNDER THE SURFACE

How it works.

An if condition converts its value to a boolean. The common falsy values are false, 0, -0, 0n, '', null, undefined, and NaN. An empty array or object is still an object value and therefore truthy. If you mean 'contains at least one item', test array.length > 0; do not rely on the array's truthiness.

Logical operators use short circuit evaluation. a && b returns a when a is falsy; otherwise it evaluates and returns b. a || b returns a when a is truthy; otherwise it evaluates and returns b. The right side may never run. This is useful for small fallback expressions, but a hidden function call on the right can make program behavior surprising, so keep important effects explicit.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const items = [];
const name = ' Ada ';
console.log(Boolean(items)); // true
console.log(name && name.trim()); // 'Ada'

First notice. An empty array exists as an object; its length is a separate question.

items is a newly created array, so Boolean(items) yields true even though its length is zero. In name && name.trim(), if name is an empty string, && stops and returns that empty string. If name is a nonempty string, it evaluates trim and returns the trimmed result. The expression does not guarantee a boolean.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const tasks = [];
if (tasks.length > 0) {
  console.log('has tasks');
} else {
  console.log('empty');
}

The output is 'empty'. The condition asks the precise question: whether the array contains an element. Replacing tasks.length > 0 with tasks would run the first branch because the array object itself is truthy.

06 / KEEP THIS

One thing to remember.

Truthiness asks whether a value counts as true, not whether it contains anything.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.