ProgramGuru

Truthiness and Missing Values

Truthiness and Missing Values

Conditions convert values to a boolean. Zero is falsy but can be a valid mark, so nullish coalescing is often a better missing-value fallback than logical OR. It falls back only for null or undefined.

Before this topic, review Numbers, Math and Rounding if its terms are unfamiliar.

What you’ll learn

  • Explain truthiness and missing values through the school example.
  • Trace the shown operations and distinguish their result from displayed output.
  • Make the practice change and explain its consequence: Both operations use the fallback for null, while only OR discards a valid zero.

Work through the school example

This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.

JavaScript · school.js · complete example

const marks = 0;
console.log(marks || "Missing");
console.log(marks ?? "Missing");

Expected output

Missing
0
Run in JavaScript compiler →

Recognize the important boundary

Empty strings and NaN are also falsy; empty arrays and objects are truthy. Nullish coalescing does not validate the value's type or range.

Try a small variation

JavaScript · original source for practice

const marks = 0;
console.log(marks || "Missing");
console.log(marks ?? "Missing");

Your task: Replace marks = 0 with marks = null. Predict the result before running the edited program.

The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.

Worked answer and reasoning

Both operations use the fallback for null, while only OR discards a valid zero.

JavaScript · complete worked variation

const marks = null;
console.log(marks || "Missing");
console.log(marks ?? "Missing");

Expected output

Missing
Missing
Run in JavaScript compiler →

Predict the original program

JavaScript · original program for this question

const marks = 0;
console.log(marks || "Missing");
console.log(marks ?? "Missing");
What does the original JavaScript program display, in order?
Choose one answer. You can try again.

Answer and explanation

Missing → 0

Conditions convert values to a boolean. Zero is falsy but can be a valid mark, so nullish coalescing is often a better missing-value fallback than logical OR. It falls back only for null or undefined. Empty strings and NaN are also falsy; empty arrays and objects are truthy. Nullish coalescing does not validate the value's type or range.

Keep the useful idea

Conditions convert values to a boolean. Zero is falsy but can be a valid mark, so nullish coalescing is often a better missing-value fallback than logical OR. It falls back only for null or undefined.

Check your variation against this explanation: Both operations use the fallback for null, while only OR discards a valid zero.

Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Make Strict-Mode Errors Visible →