sort function of javascript array object take 1 parameter or no parameter.
what would happen if no paramerter? it sort alphabetically. in order to tell javascript howyou want to sort you need to pass a function as parameter to sort function. The parameter function takes two arguments and either zero or positive non zero or negative non zero (haha)
funtion comp(a, b) {
return a-b;
}
arr.sort(comp);
in your example your function is anonymous. this fuction is not reusable and will be lost after execution
I will give you an example, so that you can understand anonymous function.
Let's say we have a function that produce "hello world". But the function itself do not render the result. Instead function uses provided mechanism to represent data.
function HelloWorld(func) {
var result="Hello World";
func(result);
}
function DisplayToDiv(res) {
document.getElementById("divid").innerHtml = res;
}
HelloWorld(DisplayToDiv);
HelloWorld(function (res){
console.log(res);
});
HelloWorld(alert);
Hope this helps
Oh!! by the way, this function that you pass is called
call back function