DEV Community

Discussion on: Array '[]'

Collapse
 
kwiat1990 profile image
Mateusz Kwiatkowski • Edited

If you use arrows functions, then I don't see any reason why not to use find instead of forEach. It's simple and more elegant than the forEach loop. Besides, I would rather say this:

fruits[0] = 'apple' 
fruits[1] = 'orange'

is not the ordinary way you add elements to the array.

Collapse
 
bladesensei profile image
coyla

Yes you are right for both suggestions,
Usually we use push as basic operation to add element.
I will remain forEach to show that loop is also used to get many elements by condition then i will add find method example too. Thanks

Collapse
 
kwiat1990 profile image
Mateusz Kwiatkowski

I would say that depends on what you need to do, it might be better to use either map or filter, which return a new array. It comes very handy in almost every situation. I used those both methods very often.