0% found this document useful (0 votes)
3 views2 pages

Rer

In JavaScript, conditions in constructs like if are coerced to booleans, with specific values such as 0, NaN, and null evaluating to false, while others evaluate to true. The document discusses using truthiness checks to prevent errors, particularly in functions like printAll, but warns against potential pitfalls, such as incorrectly handling empty strings. It also highlights the importance of TypeScript in catching bugs and suggests using a linter for better error handling.

Uploaded by

kitow93602
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Rer

In JavaScript, conditions in constructs like if are coerced to booleans, with specific values such as 0, NaN, and null evaluating to false, while others evaluate to true. The document discusses using truthiness checks to prevent errors, particularly in functions like printAll, but warns against potential pitfalls, such as incorrectly handling empty strings. It also highlights the importance of TypeScript in catching bugs and suggests using a linter for better error handling.

Uploaded by

kitow93602
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

In JavaScript, constructs like if first “coerce” their conditions to booleans to

make sense of them, and then choose their branches depending on whether the result
is true or false. Values like

0
NaN
"" (the empty string)
0n (the bigint version of zero)
null
undefined
all coerce to false, and other values get coerced to true. You can always coerce
values to booleans by running them through the Boolean function, or by using the
shorter double-Boolean negation. (The latter has the advantage that TypeScript
infers a narrow literal boolean type true, while inferring the first as type
boolean.)

// both of these result in 'true'


Boolean("hello"); // type: boolean, value: true
!!"world"; // type: true, value: true
This kind of expression is always truthy.
Try
It’s fairly popular to leverage this behavior, especially for guarding against
values like null or undefined. As an example, let’s try using it for our printAll
function.

function printAll(strs: string | string[] | null) {


if (strs && typeof strs === "object") {
for (const s of strs) {
console.log(s);
}
} else if (typeof strs === "string") {
console.log(strs);
}
}
Try
You’ll notice that we’ve gotten rid of the error above by checking if strs is
truthy. This at least prevents us from dreaded errors when we run our code like:

TypeError: null is not iterable


Keep in mind though that truthiness checking on primitives can often be error
prone. As an example, consider a different attempt at writing printAll

function printAll(strs: string | string[] | null) {


// !!!!!!!!!!!!!!!!
// DON'T DO THIS!
// KEEP READING
// !!!!!!!!!!!!!!!!
if (strs) {
if (typeof strs === "object") {
for (const s of strs) {
console.log(s);
}
} else if (typeof strs === "string") {
console.log(strs);
}
}
}
Try
We wrapped the entire body of the function in a truthy check, but this has a subtle
downside: we may no longer be handling the empty string case correctly.

TypeScript doesn’t hurt us here at all, but this behavior is worth noting if you’re
less familiar with JavaScript. TypeScript can often help you catch bugs early on,
but if you choose to do nothing with a value, there’s only so much that it can do
without being overly prescriptive. If you want, you can make sure you handle
situations like these with a linter.

One last word on narrowing by truthiness is that Boolean negations with ! filter
out from negated branches.

function multiplyAll(
values: number[] | undefined,
factor: number
): number[] | undefined {
if (!values) {
return values;
} else {
return values.map((x) => x * factor);
}
}

You might also like