JavaScript functions travel first class

Search for a command to run...

Functions in JavaScript are first-class citizens of the language just like Objects.
Functions enjoy all the same privileges (in fact more) as do the Objects in JavaScript.
Privileges such as
var obj = {a : 2, b: 2}; // object literal
var add = function(a, b) { // function literal
return a + b;
};
var add = function(a, b) { // function assigned to a variable
return a + b;
};
var arr = [];
arr[0] = add; // function assigned to array entry at index 0
var obj = {};
obj.addMethod = add; // function assigned to Object obj as a addMethod property
var values = [34, 56, 12, 11 , 5, 123];
var descendingComparator = function(value1, value2) {
return value2 - value1;
}
values.sort(descendingComparator); // function descendingComparator passed as argument
console.log(values); // this should print [123, 56, 34, 12, 11, 5]
function multiplier(x) {
return function(y) { return x * y;};
}
var doubleIt = multiplier(2);
console.log(doubleIt(2)); // prints 4
var tripleIt = multiplier(3);
console.log(tripleIt(3)); // prints 9
function myFunc() {}
myFunc.prop1 = 'prop1';
console.log(myFunc.prop1); // prints prop1
In addition to all of the above, Functions also have a special superpower in that they can be invoked