January 2020
Intermediate to advanced
470 pages
11h 13m
English
The first example of passing functions as parameters is provided by the Array.prototype.sort() method. If you have an array of strings and you want to sort it, you can just use the sort() method. For example, to alphabetically sort an array with the colors of the rainbow, we would write something like the following:
var colors = [ "violet", "indigo", "blue", "green", "yellow", "orange", "red"];colors.sort();console.log(colors);// ["blue", "green", "indigo", "orange", "red", "violet", "yellow"]
Note that we didn't have to provide any parameters to the sort() call, but the array got sorted perfectly well. By default, this method sorts strings according to their ASCII internal representation. So, if you use this method ...
Read now
Unlock full access