I'm learning javascript and a I got some questions about the sort method in javascript, yes, before you ask I've read the other questions, I understand this:
"Less than 0: "a" is sorted to be a lower index than "b". Zero: "a" and "b" are considered equal, and no sorting is performed. Greater than 0: "b" is sorted to be a lower index than "a"."
Thats from the main question about the sort() function, but what I don't understand is:
var array=[5, 2, 1, 10]
array.sort(function(a,b) {
return a - b})
What is the purpose of the a
and b
as parameters in the function, what are value for the parameters that are going to be used during the function? It is told to return a-b
but who are going to be a
and b
during the process? I'm not asking for the console.log()
example. If a
and b
are my paremeter how is the function going to work if I am not even passing the value of a
and b
?
In other languages it would be neccesary to pass the values of a
and b
.