Truthy and Falsy Values in Conditions

A zero mark is still a recorded mark

A report should display a real zero, but use “Not entered” for a missing mark. Treating every value that behaves like false as missing would erase valid results.

A truthy value behaves like true in a boolean context; a falsy value behaves like false. This conversion is a language rule, not a judgment about whether the data is valid.

  1. Predict a condition

    Recognize common JavaScript falsy values and nonempty strings.

  2. Preserve zero

    Choose a missing-value fallback without erasing valid marks.

  3. Avoid misleading checks

    Distinguish truthiness, emptiness and explicit boolean meaning.

Three values that need different interpretations

  1. Known zero

    Zero is falsy but can be a valid result.

    0
  2. Missing mark

    The program deliberately uses null for unentered results.

    null
  3. Textual answer

    The word false in quotes is nonempty text and is truthy.

    "false"
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

A boolean context applies conversion rules

In JavaScript, false, 0, -0, 0n, an empty string, null, undefined and NaN are falsy. Most other values are truthy, including empty arrays, empty objects and strings such as “0” and “false.”

if uses this conversion when its condition is not already a boolean. The value is not necessarily reassigned or permanently converted. Use explicit comparisons when the school rule concerns a specific value rather than broad truthiness.

Before starting, review logical operands and fallbacks. These examples use JavaScript; other languages can use different syntax and rules.

Keep zero when selecting a fallback

|| returns its right operand when the left is falsy, so numeric zero is replaced with Not entered. ?? replaces only null or undefined, so the recorded zero is preserved.

The final output is true because the nonempty text “false” is truthy. A checkbox boolean and a text answer are different representations.

JavaScript · local teaching example

const mark = 0;
console.log(mark || 'Not entered');
console.log(mark ?? 'Not entered');
console.log(Boolean('false'));

Expected output

Not entered
0
true

An empty array is not a falsy value

An array is an object, and JavaScript objects are truthy. Boolean([]) is true even though the list has no student records.

If the task asks whether the list contains students, check its length. Truthiness and nonempty collection size are different conditions.

JavaScript · local teaching example

const students = [];
console.log(Boolean(students));
console.log(students.length > 0);

Expected output

true
false

Apply the rule to a small change

A mark is explicitly null because it has not been entered. Display a fallback with ??, then compare it with a real zero.

Worked answer

The null mark uses Not entered, while numeric zero remains zero. This matches the chosen missing-data convention.

JavaScript · local teaching example

const missingMark = null;
const zeroMark = 0;
console.log(missingMark ?? 'Not entered');
console.log(zeroMark ?? 'Not entered');

Expected output

Not entered
0

Recognize mistakes and limits

Do not use if (mark) as a complete numeric validity check: it rejects zero but accepts many unsuitable truthy values. Check presence, finite numeric type and range deliberately.

Do not parse the word “false” with Boolean(text). Interpret allowed text choices explicitly. Also remember that && and || can return operands, and ?? must be parenthesized when mixed directly with && or || in JavaScript.

For the language or tool rules used here, see JavaScript control flow.

Check the core idea

JavaScript · code for this question

const mark = 0;
console.log(mark || 'Not entered');
console.log(mark ?? 'Not entered');
console.log(Boolean('false'));
Why is || unsuitable for the report’s zero-mark fallback?
Choose one answer. You can try again.

Answer and explanation

It treats valid zero as falsy and replaces it.

Yes. The task needs a missing-value fallback, not a fallback for every falsy value.

Apply the idea to a new situation

JavaScript · code for this question

const students = [];
console.log(Boolean(students));
console.log(students.length > 0);
How should you check whether an array contains students?
Choose one answer. You can try again.

Answer and explanation

Check students.length > 0.

Yes. This directly asks whether there are entries.

Test the property your task actually needs

Truthiness is a conversion rule. It is not the same as a value being present, a collection being nonempty or a mark being valid. Use ?? for a null-or-undefined fallback and explicit checks for the real data contract.

Connect this idea with input validation and operand-returning logical expressions.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Loops: Repeat Work and Track State →