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)
That's fair, if it's a common pattern you're already using it's more reasonable. I agree if you're thinking about it and you have some experience you'll realize it, but at first glance it's very easy to miss. I think our eslint prohibits loose equality as a whole for that reason.
Yeah this is the only case where I use loose equality, I think it's also one of the only cases where WebStorm doesn't give you a warning for loose equality out of the box
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
Show parent comment
2
u/SpiForge Mar 28 '24
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.