DEV Community

Discussion on: Enhance your JS Skill by using the correct array method

 
jonrandy profile image
Jon Randy 🎖️ • Edited

From the MDN docs for filter:

Function is a predicate, to test each element of the array. Return a value that coerces to true to keep the element, or to false otherwise.

Thread Thread
 
sebalr profile image
Sebastian Larrieu

I know the docs, I know js and I know coertion, I also know this kind of things lead to bugs and thats why we have lint rules and typescript.
If you want to filter strings and abuse coertion you will lost one valid result from this array [null, '', 'valid'], just because one smart developer didnt want to write some extra text

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

You should probably email Mozilla to get them to change their docs - they're condoning abuse!

Seriously though, there's nothing wrong with doing it if you understand the ramifications. Sure, you gave an example with a very particular situation where there might be an issue, but there are also times when you KNOW what data is going to be coming in, and can code appropriately.

Appropriate code for appropriate situations

Thread Thread
 
sebalr profile image
Sebastian Larrieu

The docs are fine because they reflect reality. But js has many parts that work in weird ways that should be avoid to write maintanible code and make life a little easier for the person that is working in your code after you

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

If we always encourage developers to avoid the quirks, and sweep them under the carpet pretending they don't exist - knowledge of JS over the years will get progressively worse (something that I've actually seen happening over years of interviewing candidates), and you're denying the developers the chance to be better developers through a more complete understanding of the language. That is a bad idea. Learn about the quirks - learn why they're good, learn why they're bad, learn how they can help you, use them to your advantage when you can.