This means that you can store, read, and call functions like objects. Therefore, they call functions “first-class citizens”: there is no different relationship between data and functions (at least at the conceptual level, obviously, the runtime can implement it differently). In most static typed object-oriented languages, such as older versions of Java, this is not possible (easy). You can pass functions as arguments, store functions in objects, and even (which makes JavaScript pretty special), print the implementation of the function.
Examples
Passing them as arguments:
you can pass the function as an argument to (for example) another function:
function foo (f,x) { return f(x);
here f is a function.
Preservation:
You can save the function in a variable:
var f = function (x) { return x+2 };
now you can call f(2) to get 4 .
print function :
you can get the signature and implementation of the self-implemented function using the .toString method. For example, using node :
> console.log(f.toString()); function (x) { return x+2 }
(obviously, the above examples are pretty simple and don't make much sense, but imagine that f , for example, updates a text box on a web page or does a complex query ...). Hope you can appreciate the power of this.
Other programming languages
Especially with older versions of Java, you could not do this. For example, a code snippet, for example:
//This is Java code to make an analogy public class Foo { public static int Bar (int x) { return x+2; } }
You cannot save Foo.Bar into a variable, pass a function to another method ... Most (object-oriented) programming languages ​​made a clear distinction between data and functions. Obviously, there are pros and cons for processing data and functions the same or different, although looking at the evolution of programming languages, I would say that treating them the same way seems to be the direction in which the community is developing (obviously, not all, and this is only a personal expression).
Programming languages ​​that definitely see functions as first-class citizens are functional programming languages ​​such as Haskell, where there are - conceptually speaking - there are no objects other than functions.