Join two questions into one condition with && and ||, turn an answer around with !, and learn the six values a condition reads as false. See why a space and “0” are truthy, why && stops before .trim() can run on a missing task, and make TaskKit refuse a task that is only spaces.
TypeScript
Fundamentals
Logical operators, truthy and falsy values. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
In the last lesson TaskKit learned to answer add with no task: it
prints a hint instead of stopping. Now give it a task made of
nothing: node index.ts add " ". TaskKit prints Added: , with
nothing after it, then Tasks added: 1 of 5, room for 4 more. Three
spaces in quotes are still a word to process.argv (argv, short
for argument vector, the list of words you typed), so the check
"was a task typed?" says yes, and an empty task joins the list.
One question is not enough here. You need two at once: was a task
typed, and is it more than spaces? A plane lets you board with a
ticket and a passport; one without the other gets you nowhere.
A café takes cash or a card; either one will do. A condition
can join questions the same way. By the end of this lesson you
join questions with and, or and not, you know which values
an if treats as false, and TaskKit refuses a task that is only
spaces.
&& and ||: two questions in one condition
TaskKit should add a task only when two things are true: a task was
typed, and the list has room. You could put one if inside
another's block, as the last lesson showed blocks can nest, but
every extra question would push the code one level deeper. A
logical operator joins the answers of two questions into one
answer, so one if can ask both.
&&, read and, gives true only when both sides are
true. The ticket and the passport.
||, read or, gives true when at least one side is
true. Cash or a card: it is false only when both are.
There are four ways two answers can come out, and here is what
each operator makes of them:
Each side is a whole question of its own, usually a comparison
from Module 2. Here is the question the last lesson promised: is
there a task, and is the list not full?
It prints Added: buy milk. Press Edit, change added to 5,
and run it: Not added, because the second answer is false. Put
it back to 3 and make the task "": Not added again, this time
because of the first. Only when both are true does the if
block run.
No parentheses are needed around the two comparisons. The
comparisons are worked out first, and && joins their answers, just
as * goes before +. When one condition mixes && and ||, &&
goes first, the same way; add parentheses to say what you mean: (a || b) && c.
|| is for questions where either answer will do. A person asking
TaskKit for help may type help or just h:
The first question is false and the second is true, so ||
gives true and the help line is printed.
!: turn an answer around
Sometimes the question you have is the opposite of the one you
want. You have a box full, and you want to say something when
the list is not full. !, read not, turns true into
false and false into true. It goes in front of what it turns
around, with no space:
room.tsts
const limit = 5;let added: number = 3;const full = added === limit;console.log(full);console.log(!full);if (!full) { console.log(`Room for ${limit - added} more`);}
It prints false, then true, then Room for 2 more. You have
met the idea before: !== from Module 2 is "not equal", the same
not built into one operator. added !== limit and
!(added === limit) ask the same thing, and the first is easier
to read.
Truthy and falsy: any value as a condition
So far every condition has been a boolean: a comparison, or a box
that holds one. But a condition can be any value. Write if (task)
with a string in the box, and the program carries on: it reads the
string as true or false and runs the block or skips it.
Most values count as true. Only six count as false, and they are
worth knowing by heart, because each one means "nothing there" in
its own way:
Value
What it is
false
The boolean itself
0
The number zero
""
The empty string, with no characters at all
NaN
"Not a number", from Module 2's lesson 5
undefined
"Nothing here", as a missing word in argv
null
A second "nothing here", next lesson's topic
A value that counts as false is called falsy. Every other value is
truthy: it counts as true. (JavaScript has two rarer forms of zero
that are falsy too; this level never needs them.)
The surprises are in the strings. Only the empty string is falsy. A
string with one space has a character in it, so it is truthy, and so
are "0" and "false": they are text, not the number or the
boolean they look like. The figure below asks if (!task), if
there is no task, using the ! from the last section. Choose a
value on the tabs, then press Next step to see where it lands,
or let it play:
Every string task can hold
""
" "
"buy milk"
"0"
"false"
if (!task)""""›prints Add what?
elseany other string""›prints Added: …
Only the empty string is falsy; a space is a character
All 3 steps shown.Step 1 of 3: The value: ""Step 2 of 3: !task: trueStep 3 of 3: "" lands in "": Add what?
All 3 steps shown.
Every string task can hold
""
" "
"buy milk"
"0"
"false"
if (!task)""" "›prints Add what?
elseany other string" "›prints Added: …
Only the empty string is falsy; a space is a character
All 3 steps shown.Step 1 of 3: The value: " "Step 2 of 3: !task: falseStep 3 of 3: " " lands in else: Added: …
All 3 steps shown.
Every string task can hold
""
" "
"buy milk"
"0"
"false"
if (!task)"""buy milk"›prints Add what?
elseany other string"buy milk"›prints Added: …
Only the empty string is falsy; a space is a character
All 3 steps shown.Step 1 of 3: The value: "buy milk"Step 2 of 3: !task: falseStep 3 of 3: "buy milk" lands in else: Added: …
All 3 steps shown.
When you are not sure how a value will be read, ask the program.
Boolean() gives back the true or false a condition would
see, the way Number() gives back the number in a piece of text:
It prints false, true, true, false, false, true: the
empty string, zero and NaN are falsy; the space, the text "0"
and the number 5 are truthy.
The second question is asked only when needed
&& and || work from left to right, and they stop as soon as
the answer is certain. If the left side of && is falsy, the
whole answer is already false, so the right side is never worked
out at all. If the left side of || is truthy, the answer is
already true, and the right side is skipped the same way. This is
called short-circuiting: the operator takes the short way
round when it can.
That is not only quicker. It decides whether TaskKit survives a
missing task. Here is the question TaskKit needs:
index.tsts
if (typedTask && typedTask.trim() !== "") {
The left side, typedTask on its own, asks "was a task typed?".
When nothing was typed, the box holds undefined, which is falsy.
The right side, typedTask.trim() !== "", asks "is it more than
spaces?", and it could only ever work on a real string: Module 2's
lesson 8 showed trim() on undefined stopping the program with
a TypeError. Choose what was typed on the tabs and press Next
step to follow the two questions:
node index.ts add …: reachedThe words you typed
typedTask: passes✓Was a task typed?
typedTask.trim() !== "": passes✓More than spaces?
Added: Buy milk: reachedBoth answers were true
&& stops at the first falsy side: .trim() runs only if a task
was typed
All 6 steps shown.Step 1 of 6: node index.ts add …Step 2 of 6: typedTaskStep 3 of 6: typedTask: passesStep 4 of 6: typedTask.trim() !== ""Step 5 of 6: typedTask.trim() !== "": passesStep 6 of 6: Added: Buy milk
All 6 steps shown.
node index.ts add …: reachedThe words you typed
typedTask: passes✓Was a task typed?
typedTask.trim() !== "": stops here✕More than spaces?
Added: Buy milk: never reachedBoth answers were true
&& stops at the first falsy side: .trim() runs only if a task
was typed
All 5 steps shown.Step 1 of 5: node index.ts add …Step 2 of 5: typedTaskStep 3 of 5: typedTask: passesStep 4 of 5: typedTask.trim() !== ""Step 5 of 5: typedTask.trim() !== "": stops here. Only spaces, false: Add what?
All 5 steps shown.
node index.ts add …: reachedThe words you typed
typedTask: stops here✕Was a task typed?
typedTask.trim() !== "": never reachedMore than spaces?
Added: Buy milk: never reachedBoth answers were true
typedTask stops hereNo task, falsy: Add what? .trim() never runs
&& stops at the first falsy side: .trim() runs only if a task
was typed
All 3 steps shown.Step 1 of 3: node index.ts add …Step 2 of 3: typedTaskStep 3 of 3: typedTask: stops here. No task, falsy: Add what? .trim() never runs
All 3 steps shown.
The order of the two sides is part of the program: the question that
protects, the guard, comes first, and the question that needs
protecting comes after it.
|| gives back a value
There is one more thing || does that a beginner rarely expects. It
does not always give back true or false: it gives back one of
its two sides. || gives back its left side if that is truthy, and
its right side otherwise:
It prints friend, then Ana. In an if that makes no
difference, because whatever comes back is then read as truthy or
falsy. But it explains the mistake people make most often with
||.
Bad — reads like English, and
runs the block for every command.
The first version prints Known command: remove. When
command === "add" is false, || gives back its right side, the
string "help", which is truthy, so the block always runs. It
passes the check without a word: TypeScript's always-truthy check
looks at a bare value such as if ("help") and at the left side of
||, never at the right side, and TaskKit would welcome every word
anyone types. Write the value you are comparing on both sides.
TaskKit refuses a blank task
Now TaskKit's step for this lesson. Open index.ts in
~/projects/taskkit. The add case asked process.argv.length < 4, which only counts the words; replace the whole add case with
this one, which asks about the task itself:
Two things changed. The condition on line 3 is the one you
followed above, so it is now a question about the task: a missing
one and one of spaces both get the hint. And because the question
is now "is there a real task?", the two blocks swapped places: the
tidy-and-add lines come first, the hint goes in the else. The
help case, the default and everything below the switch stay
as they were.
Check the file with tsc (short for TypeScript compiler), which
npx starts, then try a real task:
============================TaskKit 0.1Your tasks, in your terminal============================Added: Buy milkTasks added: 1 of 5, room for 4 moreList 20% full
Now the run this lesson started with:
bash
node index.ts add " "
Outputtext
============================TaskKit 0.1Your tasks, in your terminal============================Add what? Try: node index.ts add "buy milk"Tasks added: 0 of 5, room for 5 moreList 0% full
And no task at all, node index.ts add, prints the same hint:
typedTask is undefined, and && stops before .trim().
Predict before you run
The right side of && here is not a comparison but a string: the
trimmed task. Decide what each side is read as, then what the
program prints.
npx tsc --noEmit room.ts refuses line 3 with TS2365. The
comparisons are worked out left to right: 0 < added gives
true, and the second < is then asked to compare true with
limit, a boolean with a number.
Operator '<' cannot be applied to types 'boolean' and 'number'.
The fix
Ask the two questions separately, each with added in it, and
join the answers with &&:
When TS2365 names a boolean on a line with two comparisons, look
for a range. Plain JavaScript would run the first version without
complaint and get it wrong: with added at 9, 0 < 9 is true,
JavaScript turns true into 1 to compare it with a number, 1 < 5
is true, and it prints Some room used for a list that is over
full.
Pass all five tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
As before, where a program would read process.argv, the value it
would hold is put straight into a box, with a comment saying where
it came from.
Exercise · Read the code
Which string is falsy
Which value of task makes this program print Add what??
ts
if (!task) { console.log("Add what?");} else { console.log("Added:", task);}
Exercise · Fix the error
Check a range of minutes
An estimate must be more than 0 and at most 120 minutes.
TypeScript refuses this program. Fix the condition so it prints
Estimate: 45 minutes.
Loading the editor…
Exercise · Fix the error
Ask a whole question on both sides
This program says every status is known. Fix the condition so
that, with later, it prints Unknown status: later.
Loading the editor…
Exercise · Write the code
Add only a real task with room for it
Write an if / else that prints Added: buy milk when
typedTask is more than spaces andadded is below limit,
and Not added otherwise. With the values below it should print
Not added, because the list is full.
The list is full and a task was typed. Does
if (added < limit || task) run?
Check yourself
Which of these values is truthy?
Check yourself
Why does node index.ts add no longer end in a TypeError?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Logical operators, truthy and falsy values
What things are
Word
What it means
Logical operator
Joins or turns around answers: &&, ||, !
&&
And: true only when both sides are
||
Or: true when at least one side is
!
Not: turns true into false and back
Falsy
A value a condition reads as false
Truthy
Any value that is not falsy
Short-circuiting
Stopping once the answer is certain
Boolean(value)
The true or false a condition would see
Code
Write
To
task && added < limit
Both must be true: a task, and room
!task || full
One is enough: no task, or a full list
if (!full) {
Run the block when the answer is false
if (task) {
Run the block when task is truthy
Boolean(" ")
See how a value is read: true
name || "friend"
name, or "friend" when name is falsy
0 < added && added < 5
Ask whether a number is in a range
Rules
Rule
What it means
Six falsy values
false, 0, "", NaN, undefined, null
Text with characters
" ", "0" and "false" are truthy
Left to right
The right side runs only when it is needed
Guard first
typedTask && typedTask.trim(), never swapped
Whole questions
x === "a" || x === "b", not || "b"
TS2365
Two comparisons in a row: join them with &&
TS2872
A condition that is always true, like if ("help")
Where to go next
You can now join two questions into one condition with && and
||, turn an answer around with !, and say which values a
condition reads as false: false, 0, "", NaN, undefined
and null. You know that && and || stop as soon as the answer
is certain, and TaskKit uses that to refuse a missing or blank
task without ever calling .trim() on nothing.
Next is null and undefined. Two of the six falsy values
mean "nothing here", and TaskKit has already met one of them in a
box whose type says string. There you find out what each one
means, where each one comes from, and how they differ.
Before you move on, give TaskKit one more rule: a task longer than
40 characters is refused with Keep it under 40 characters. Decide
whether it belongs inside the add condition with another &&, or
in an else if of its own with its own message, then run
node index.ts add with a long task and predict the output before
you press Enter (return on a Mac).