![]() They always yield consistent result and does not manipulate non-local state or have any side effects. Pure functions are building blocks of functional programming. Their return value is just dependent on the input arguments, they don't mutate any non-local state and they have no side effects (we will discuss side-effects further in this article). Share the knowledge with your fellow developers!ĭo you know of more JavaScript gems that could help the JS community write cleaner, simpler code? If so, leave a comment and I will try to write a blog post about it.All three functions in the above code are pure functions. some(), you are clearly checking if the array contains items that fit your needs or not. find() states that you are looking for one particular array item. A loop could be used to do anything, but using. ![]() forEach() not only makes your code shorter, but also makes your intent clearer. If you need a list of all matches, then you should use. If more values match your condition, it won’t matter. some(), the only difference is that we changed some for find ! But now, instead of returning a boolean, it will return the first pilot in our list. ![]() Here’s a list of operatives working for your super secret organization: var operatives = [ ) Įven shorter with ES6’s arrow functions: const firstPilot = operatives.find(operative => operative.pilot) Īs you can see the code is exactly the same as with. If that doesn’t sound clear to you, let me illustrate with an example… This array method helps you determine if one or more of its values correspond to something you’re looking for. filter(), I thought I should share two more array methods that are useful tools in a JavaScript developer’s arsenal. find()įollowing the insane amount of views on my article on.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |