LESSON 08 · BEGINNER4 min read

Booleans, Comparisons & Truthiness

Compare values safely, predict which values count as true, and use logical operators without hidden conversions.

ES2024 / Node.js 20+Free to readModule 2 of 19
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Use comparison operators and choose strict equality.
  • Classify values as truthy or falsy.
  • Apply &&, ||, and ! while tracking short-circuit behavior.
  • Use nullish coalescing and recognize optional chaining.

01Comparisons produce booleans

Comparison operators produce booleans: >, <, >=, <=, ===, and !==. Use the triple-equals forms for equality and inequality. Strings compare character by character using UTF-16 code units, which makes uppercase letters sort before lowercase letters. That is why "Z" < "a" is true.

A comparison is an expression, so it can be stored in a variable or printed. Do not confuse =, which assigns, with ===, which compares. Reading isReady === true is repetitive; when a value is already boolean, use it directly in a condition. The next lesson's conditionals build on these results.

comparisons.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

02Strict equality avoids surprises

The loose equality operator == converts operands before comparing, which creates rules that are hard to remember. 0 == "0" is true, and null == undefined is true, while the strict versions are false. Loose equality is a frequent source of bugs in real code.

Prefer === and !== in every new program. They compare type and value without hidden conversion. The one common exception is checking for null or undefined together with value == null, but even that is clearer as an explicit check once you know the nullish operator. Consistency beats cleverness. When in doubt, choose the strict operator and convert explicitly.

strict-equality.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

03Truthy and falsy values

A value is truthy or falsy when it is used as a condition. The falsy values are false, 0, -0, 0n, an empty string, null, undefined, and NaN. Everything else is truthy, including the strings "0" and "false", non-empty arrays, and objects.

This surprises beginners because "0" looks like zero. The string contains a character, so it is truthy. Use Boolean(value) in the console to inspect a value's truthiness while learning. In production, prefer an explicit comparison when the difference between zero and missing matters.

truthiness.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

04Logic depends on returned values

The && operator returns its first falsy operand or its last operand. The || operator returns its first truthy operand or its last operand. Both short-circuit: the right side is not evaluated when the result is already decided. That behavior can avoid unnecessary work.

These operators return values, not necessarily booleans. "first" && "second" evaluates to "second", and 0 || 10 evaluates to 10. The || fallback also replaces 0 and an empty string, which may be valid data. When only null and undefined should trigger a fallback, use ??.

logical-values.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

05Nullish coalescing and optional chaining

Nullish coalescing with ?? chooses the right side only when the left side is null or undefined. That makes it safer than || for numbers and empty strings. Optional chaining with ?. reads a property only when the object exists, returning undefined instead of throwing.

For example, profile.contact?.email returns undefined when contact is missing. These operators are previews of ideas covered later, so use them in small, readable expressions. Do not mix ?? with || or && without parentheses; JavaScript requires the grouping to be explicit.

safe-reads.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
LET IT CLICK

The ideas worth keeping.

  • Use === and !== to compare type and value without hidden coercion.
  • The falsy set is small; strings such as "0" and "false" are truthy.
  • && and || return operands, while ?? falls back only for null and undefined.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Build a truthiness table

Print a labeled line showing Boolean for each of these values: 0, "0", "", "false", null, and NaN. Use one console.log call per value.

your_turn_1.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
console.log("0:", Boolean(0));console.log("text zero:", Boolean("0"));console.log("empty:", Boolean(""));console.log("false text:", Boolean("false"));console.log("null:", Boolean(null));console.log("NaN:", Boolean(NaN));

The output is false, true, false, true, false, false in that order. The two strings that look false contain characters, so they are truthy.

02

Exercise 2: Choose safe fallbacks

With username = "", nickname = null, and score = 0, print a display name using || so the result is Guest. Then print score with ?? and with || on separate lines so the difference is visible.

your_turn_2.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
const username = "";const nickname = null;const display = username || nickname || "Guest";console.log(display);const score = 0;console.log(score ?? 100);console.log(score || 100);

The display name falls through two falsy values to Guest. For score, ?? keeps 0 because it is neither null nor undefined, while || replaces it with 100.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01Which value is truthy?
02What is the result of 0 == "0"?
03If score is 0, what do score || 10 and score ?? 10 return?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

Exercise 1: Build a truthiness table

Print a labeled line showing Boolean for each of these values: 0, "0", "", "false", null, and NaN. Use one console.log call per value.

EXPECTED OUTPUT
0: false
text zero: true
empty: false
false text: true
null: false
NaN: false
main.jsJavaScript
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock lesson completion.