foreach() 

The for and while loops can be generally used for any type of loop, but lists also have some specific methods that help you write elegant and readable code.

The foreach method of a list lets you run a function on each element in the array. So, you could delete the while loop and use the following code instead, in order to achieve the same result: 

songs.forEach((song) => songString += song + " - ");

The foreach method takes a function as a parameter. This function may be anonymous. This anonymous function takes an argument (song in this case), of the same data type as the list itself. So, as the songs list is a list of strings, song will be a string as well. 

You've seen the => arrow syntax in the previous topic. In this case, instead of returning a value, we are setting the value of a variable, and this is totally acceptable as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.179.100