Struggling with my investigation methods a bit.

Would someone indicate where in this documentation it explains that the Boolean "method" will return only the truthy values please? So if I use it in the following code it returns the number of truthy values in array r. It does what it does but I need to understand why it doesn't return all truth/falsey values and only the truthy ones. Now I'm a bit more confused because I'm not sure if this question even makes sense...
r.filter(Boolean).length
r.filter(Boolean).length
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean (edited)
17 Replies
13eck
13eck13mo ago
That’s because the .filter() method only return truthy values. It has nothing to do with Boolean
Fourth_quarter
Fourth_quarterOP13mo ago
Okay so would this work
const countTrue = r => r.filter().length
const countTrue = r => r.filter().length
ErickO
ErickO13mo ago
the filter method needs a function the filter method accepts a function, said function will be called with each one of the values you have on the array, if that function returns true then the value passes the filter and is added to the end result, otherwise is removed
13eck
13eck13mo ago
No, as Erick said the filter method needs a function passed in. No function means undefined so it’ll filter out everything, as undefined is falsey
Jochem
Jochem13mo ago
I'm honestly surprised this even works
No description
Jochem
Jochem13mo ago
still, it's bad practice to write hard to read code, which this definitely qualifies as.
13eck
13eck13mo ago
It’s called “points free” and isn’t that uncommon filter((x) => Boolean(x)) is the same as filter(Boolean)
Fourth_quarter
Fourth_quarterOP13mo ago
Question concerning the earlier example: This makes sense now but it seems like mental gymnastics. So I guess my next question is that for me there is a huge gap between my understanding of filter() returning values that match my constraints VS understanding that basically filter() returns truthy values. Is this how it is for most everyone? And would this type of code knowledge come the same way I was introduced to it, by seeing an example, or does it all just become clear one day? I hope my question makes sense. My mind just struggles to get the concept, and hold onto that realization, much less being able to break it down into core concepts.
ErickO
ErickO13mo ago
I think you're missing core concepts if this is confusing you to this level
Jochem
Jochem13mo ago
.filter doesn't return truthy values, it returns values for which the function you pass in returns a truthy value
Jochem
Jochem13mo ago
No description
Jochem
Jochem13mo ago
you're passing in Boolean as a function to call, which returns a truthy value when it's passed a truthy value, and a falsey value when it's passed a falsey value. It's a code golf shortcut to filter an array, that I wouldn't recommend you use yourself in production code. Write it out yourself, it's just filter(x => x == true) and it's much easier to read
Jochem
Jochem13mo ago
I do agree with Erick though, you probably want to read up and practice some more with the basics if this is so confusing. https://javascript.info is a good resource for that
Fourth_quarter
Fourth_quarterOP13mo ago
Around 2019 there was an http request tool that was only able to execute from the frontend...or maybe it was only one the could work in both front and back and the rest were limited to the back? Anyone know? ajax?
Jochem
Jochem13mo ago
Fetch? Probably fetch, nodejs added support for it relatively recently. Also please make new posts for new questions
Fourth_quarter
Fourth_quarterOP13mo ago
Thanks. Every question requires a post?! Seriously, does not qualify for a whole post, but the rules are the rules.
Jochem
Jochem13mo ago
It does because a new post will attract new people to help you that already decided they couldn't help you with this one. It also makes it easier to find answers later by searching the forum posts If you don't think it qualifies, 🤷
Want results from more Discord servers?
Add your server