r/learnjavascript • u/Sumant125 • Feb 17 '26
Counting Truthy Values: Why 1st works and 2nd does not.
I have the following exercise:
- Given:
function countTruthy(array) {} - To pass:
const array = [0, 1, 2]; - Expected output:
2 - Objective: Make the function such that the count of truthy values is returned.
Now, I made two attempts and need to know why 1st works and 2nd does not.
- After I finished the basics and re-tried (in VS Code + Live Server):
const array = [0, 1, 2];
function countTruthy(array) {
let truthyCount = 0;
for (let index in array)
if (array[index] != false) truthyCount++;
return truthyCount;
}
console.log(countTruthy(array));2. While doing the course but writing the code only on paper:
const array = [0, 1, 2];
function countTruthy(array) {
let truthyCount = 0;
for (let element of array)
if (array[element] == true)
truthyCount++;
return truthyCount;
}
console.log(countTruthy(array));I want to know why 2nd does not work.
2
Upvotes
5
u/Beginning-Seat5221 Feb 17 '26 edited Feb 17 '26
Comparing different types like
1 == trueis what I call a garbage comparison. The results are often unpredictable.When comping
0, 1, 2tofalse, 0 matches, 1 and 2 don't, so you get a count of 2.When comparing
0, 1, 2totrue, 0 doesn't not match, 1 does and then 2 does not, it seems JS considers 1 similar to true, but not 2.This gives the strange situation that != false gives 2 matches, but == true gives one match. It's a garbage comparison, don't use it.
The two types are comparison to use are
===which first checks that the types are equal, or returns false, and a truthy check. == true is not a truthy check. A truthy check isif (x)and(x) ? a : b. Usingif (array[element] == true), your second code works.There's an argument for comparisons like "5" == 5. JS is eager to convert both sides to a string with mismatched arguments, so this is fairly predictable, but I would still just do
"5" === String(5)orparseInt(5) === 5.---
P.S. your second loop works because your values coincidentally match your keys 0: 0, 1: 1, 2: 2. You are fetching the value then indexing the array with the value, which would normally result in garbage/undefineds. It's just luck here that it does not. If you're using
for ... ofyou already have the value and can use it like this:3
u/mrmiffmiff Feb 17 '26
Using for...of but using standard array-index-accessing syntax is syntactical nonsense. You're already accessing the element itself, you don't need to use it to check the array. In this case it amounts to the same thing because your array values are equal to their indices, but in most arrays that'd just break everything.
Another thing is that that's exactly what I'd expect to happen. 0 is falsy as you'd expect. 1 does == true. 2, however, does not == true, even though it's truthy. x == true only returns true if x is vaguely equivalent to 1 after type coercion. 2 obviously does not equal 1 and therefore can never == true.
The proper, idiomatic way to do that second function's check would just be
if (element)as the if statement.The first function works because, rather than checking that something is == true, it's checking that it's != false, which does return true for both 1 and 2. However, I do think using a for...in loop is also syntactically a bit odd, since you're actually looping through all enumerable properties in the array-as-an-object. While it works here, this can lead to unexpected behavior in other situations (such as if enumerable properties have been added to the array that are not its indices, or to the prototype chain) and can cause performance issues. I do not suggest using for...in with arrays unless you have a specific reason to do so.
3
u/ProfessionalPin3263 Feb 17 '26
One “simplification” you could use, and it’s something nice for you o go deeper I. js learn, would be something like this:
truthyCount += Number(!!element)
The “!!” Converts the number (element) to Boolean by double negating it. The “Number” cast will convert true to 1 and false to 0, so you’ll only add the truth ones to the counter.
5
u/SnooLemons6942 Feb 17 '26
Second one is using "for of", which iterates over the elements, and then you're trying to index the array with the element. either use "for in" or just use the "element" variable straight up
Also "truthy" doesn't mean that it's ==true. I beleive "true" is getting coerced into a 1 here. And 2 does not equal 1.
To check if something is truthy, just do "if(theThing)". Don't use equality operators
2
u/PM_ME_YOUR_BUG5 Feb 17 '26 edited Feb 17 '26
2
u/DinTaiFung Feb 17 '26
Over many years of working with JavaScript in professional settings I have discovered bugs from colleagues due to the syntactic shortcut of omitting braces to demarcate blocks of code.
The OP's code examples omit these helpful braces.
I'm merely suggesting that stylistically this code could be improved for readability.
Regarding the comparison operators, Douglas Crockford (JSON inventor and author) referred to == and != as the "evil twins."
Do not use them. Use === and !== instead.
When you eventually use Typescript you will do your best to completely avoid JavaScript's quirky truthy and falsy behaviors and use real Boolean expressions instead.
Another piece of unsolicited advice:
Use "for of" instead of "for in"
In fact, "for of" is better than .forEach() 99% of the time.
2
u/GodOfSunHimself Feb 17 '26
The problem is type coercion, the comparison is not doing what you expect. If you want to avoid such mistakes in the future then 1) don't compare values that have different types (eg numbers with booleans as you do here) and 2) don't use the == operator, always prefer ===.
2
u/ProfessionalPin3263 Feb 22 '26
Not answering, but, as you are learning JS…
One other concept (the one that took me longer to grasp and hated by a lot of people) is Array.reduce.
Reduce will apply a callback to each element with the “previous response”.
So, you could write this single line:
const result = array.reduce((previous, current) => previous += Number(!!current), 0)
The first parameter is the callback function. The second (the number 0) is the “initial previous”.
It’s weird to understand, but I find it quite elegant
10
u/v01dc0d3 Feb 17 '26
Two different type of loops
first one
i in arryou loop over indices of the array so you compare the element like thisarray[index] != falseSecond type of loop
element of arrloops with the elements, so you could just do the check like thiselement == trueLet me know if you have any questions
2
u/Lithl Feb 17 '26
That standard means to check for a truthy value (as opposed to the value
true) is to manually coerce the value to a boolean by negating it, then negate it again (!!myVar). Similarly, you can check for a falsey value with a single negation (!myVar).You end up with code that looks similar to this:
More broadly, it's not recommended to use
==or!=comparators. While their behavior is consistent and documented, it's not obvious if you haven't memorized an unreasonable amount of language specs.Use
===and!==instead, and if you find yourself with a need for a comparison between differing types, manually convert one or both values to the same type yourself so that you never have unexpected behavior.2
u/Total-Box-5169 Feb 17 '26
Use the operator ! to count falsy and !! to count truthy.
To iterate over elements use for of, or better reduce:
1
u/[deleted] Feb 17 '26
for(let element of array) will not work, will it?
-2
u/Onitwin Feb 17 '26
I think you are returning as soon as you hit the first truths value- if you take the return out of the for loop it will only run after it has iterated over the whole array, so return after the for loop and not inside it