At first it may look odd but we just avoid one extra anonymous function and provide the .filter() argument into the Boolean() function directly. Short and clean.
Boolean() will return true for truthy values and false for falsy values (null, undefined, 0, '', false).
It's a nice way to clean up arrays. Use it wisely.
PS: [] and {} are not falsy values as you may think at first...
You're right. using Boolean directly makes code shorter and is really helpful. Some days back I had written an article describing the same with truthy and falsy values.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One that I often:
At first it may look odd but we just avoid one extra anonymous function and provide the
.filter()
argument into theBoolean()
function directly. Short and clean.Boolean()
will return true for truthy values and false for falsy values (null
,undefined
,0
,''
,false
).It's a nice way to clean up arrays. Use it wisely.
PS:
[]
and{}
are not falsy values as you may think at first...Yep! That's nice one to work with array of primitives.
I had this small helper that export from helper file.
and then import it and I put it as argument in the filter method.
You're right. using
Boolean
directly makes code shorter and is really helpful. Some days back I had written an article describing the same with truthy and falsy values.