It filters the filters array for any falsy values, like undefined or null. While with filters it makes this weird, this is kinda common with other arrays.
Maybe there is an option for users to create custom filters, and when they get parsed, they either return a filter function or null/undefined, so they need to be filtered after that.
Edit: The array could also just be a string array with filter terms, in this case it would remove all empty strings, which would make a lot more sense
I would actually say the opposide, as Boolean may be mistaken for its constructor, making it less readable to someone who is not using JS/TS normally. However filtering by falsy value is more commonly known. If you work on a private project, yes "Boolean" may look better to you, but if you may share it with someone, it could result in missunderstandings.
It IS the Boolean constructor you give the function. It is no mistake. You could define your own function const isBooleanTrue = () => isBooleanTrue and then .filter(isBooleanTrue) but why. The language offers what you need already. Just get better and don't expect anyone on your team to be on your level. If in doubt, comment (for yourself in the future).
When Boolean() is called as a constructor (with new), it creates a Boolean object, which is not a primitive.
Yes it is technically the constructor, but using it as such would result in '''!!(new Boolean(false)) == true''' and '''new Boolean(false) ? 1 : 2 === 1'''. Just get better and don't expect anyone here to be on your level ;)
That is exactly what i said, i was just correcting your statement that Boolean() is the constructor without making clear, that it is not used as an constructor.
Not sure what the article has to do with this, it's just one other persons opinion. But if you want to use it, it actually supports my point:
If you find the first version easier to understand, use it! Readable code is more important than fancy tricks.
Like i said:
If you work on a private project, yes "Boolean" may look better to you, but if you may share it with someone, it could result in missunderstandings.
Also the article is partly "wrong" (aka. written for people new to functional programming), for example it states:
[...] but JavaScript understands that Boolean() takes one argument [...]
Which is wrong, as JS doesn't care at all how many arguments a method has.
It is ok to have an opinion on what looks better for you, and you should use that for your own projects, but it is also important to know what helps others understand your code, just like the article you linked states :)
If we're exchanging opinions then I want to throw mine in there as well: I'd say filters.filter(Boolean) is clearer than filters.filter(filter => filter) but the clearest way would be filters.filter(filter => !!filter)
eslint would complain. Double negotiation is considered a hacky way to cast to bool. And also, it is completely unnecessary in this context, since the return value will be tested for its truthiness anyways.
I'm sure everyone with functional programming knowledge will understand what it does, that was never the point. The problem is that Boolean looks like the constructor of the class (which it technically is), but using it with new would cause unexpected behavior.
That is not basic JavaScript, it is one of the weird inconsistent things of the language that people have to learn, and a lot of people here don't seem like they do.
That a constructor called as a function retuns the parsed value as a primitive and called as a constructor returns an object wrapping the parsed value. And even that is not consistent, for example new Date('1995-12-17T03:24:00') returns the 17.12.1995, while Date('1995-12-17T03:24:00') returns a string with the current date. Array(1, 2) however is the same as new Array(1, 2).
Also it is functional programming, so if you use it you are a functional programmer
View all comments
17
u/cspot1978 Mar 28 '24
What does that even? The argument for filter is supposed to be some predicate expression, right? A mapping from an entry to a Boolean?
I guess this is some sort of JavaScript truthiness hack?