Ok, I’ve read a ton on the arrow function and how it works. I have been able to use them a few times during this project, but really, I still don’t understand the premise. Take the following, for example:
No, I mean what use does it have? Why do we use it? What effect on the code does it have?
I have read several other posts on this forum about the arrow function and I still don’t understand any thing about it. This ended up being the correct answer to the problem in this case:
If you want to know why we need the arrow, it is because without it, the code you wrote is not a function. It is just a expression.
A function is something that can be packaged up for the purpose of using it later. An expression, (the code you write without the arrow) is just something that will get interpreted immediately.
So in the case of filter, if you pass a comparison expression, what you are actually passing is the result of that expression, ie. a true or false. That is not useful.
A function does some behaviour. It is not evaluated by the JavaScript engine when the code is read. It gets evaluated only when it is called (later). In the case of filter, it gets evaluated when we invoke it on each item in the array.
Thanks! That’s very helpful. Sorry for the late response, just ate lunch. How should I make sense of this when trying to write it into my code? When do I know to add something like (song)? The comparison was between song.id and id, so why should (song) be involved in this case?
If you mean, when do I know that I need an arrow function or a function, well that will depend on what you are doing. If you forget that filter needs an arrow function, you can look it up (and the documentation on the MDN website will say that)