4.13 Exercise: Counting Truthy Value
This exercise is about truthy and falsy values. Your task: create a function countTruthy that takes an array and returns the number of truthy elements in it. Before writing the function, let's quickly refresh what truthy and falsy mean.
Truthy vs falsy quick refresher
When a non-boolean value is used in a boolean context (an if condition for example), the JavaScript engine coerces it: it interprets the value as either truthy or falsy. The falsy values are undefined, null, 0, false, '' (empty string) and NaN. Every other value — for instance the string 'Toto' — is truthy.
Implementation
function countTruthy(array) {
let count = 0;
for (const value of array) {
if (value) count++;
}
return count;
}
const items = [1, 2, 3, 0, null, undefined, '', 'hello'];
console.log(countTruthy(items)); // 4
- The
for...ofloop iterates over the array values directly. - Writing
if (value)takes advantage of implicit boolean coercion — no need to compare against every falsy value manually. - The counter increments each time the value is truthy and is returned at the end.
The implementation stays concise because we trust the language's coercion rules. Avoid writing long chains like if (value !== null && value !== undefined && value !== 0 ...). See you in the next demonstration.
Summary
This lesson teaches how to create a function that counts the number of truthy values in an array using JavaScript. It first explains the concept of truthy and falsy values—how JavaScript evaluates non-boolean values as either truthy or falsy in conditional contexts. The exercise demonstrates a practical implementation using a for...of loop and the most efficient approach: passing values directly in an if condition without explicitly comparing them to false or other falsy values.
Key points
- Truthy values are evaluated as true in conditional statements, while falsy values are interpreted as false by the JavaScript engine, even if they are not actual boolean types
- Non-boolean values like strings, numbers, and objects are coerced to their truthy or falsy equivalents—empty strings and zero are falsy, while non-empty strings and non-zero numbers are truthy
- The countTruthy() function uses a for...of loop to iterate through an array and increment a counter when each element evaluates to true
- Avoid writing multiple conditions to check against all falsy values; instead, pass the value directly in the if statement to leverage JavaScript's automatic type coercion
- Empty strings, null, undefined, zero, NaN, and false are falsy; everything else is truthy in JavaScript
FAQ
What is the difference between a truthy value and a boolean true?
A truthy value is any non-boolean value that JavaScript interprets as true when used in a conditional statement. For example, a non-empty string like 'hello' is truthy, but it is not the boolean value true. The JavaScript engine automatically coerces non-boolean values to their equivalent truthy or falsy status.
Why should we not compare values to false or undefined in the countTruthy function?
Comparing against all falsy values requires multiple conditions (checking for false, undefined, null, 0, empty string, and NaN), which is inefficient and cluttered. Instead, we can pass the value directly in the if condition, allowing JavaScript's type coercion to automatically interpret whether the value is truthy or falsy.
What values are considered falsy in JavaScript?
The falsy values in JavaScript are: false, 0, empty string (''), null, undefined, and NaN. All other values, including non-empty strings, non-zero numbers, and objects, are truthy.