JS: Functions
Theory: First-class objects
In programming languages, there is a concept of "objects of the first kind (or class)". It refers to elements that can be passed to functions, returned from functions, and assigned to variables (or constants). Such elements include any kind of data, such as numbers, strings, arrays or logical values.
Objects of the first kind can be not only what we used to call the word "data", but also any language construct, such as functions. In JavaScript, functions are objects of the first kind. This peculiarity has a very serious effect not only on the handling of functions, but also on the general programming style. Below we will talk about treating functions as data.
Saving to a constant
There's more action in this code than we're used to thinking:
- Creating (defining) a function:
() => console.log('I love Hexlet') - Creating a constant
xand storing a value in it as a function:const x =
This moment needs to be well felt. The minimum definition of a function that is possible looks like this: () => {}. It is an empty function with an empty body that does nothing. Whether to assign it to a constant or not is a separate question.
Even if you save a function inside a constant, nothing prevents you from passing it to another constant. Just don't forget about the object nature of the function. It is not the function itself that is passed to the other constant, but a reference to it:
Moreover, any function can be used directly, without storing it in a constant:
In the example, we made the function call on the fly: first we created (() => console.log('I love Hexlet')) and immediately made the call with the function call operator (). The function definition should be wrapped in parentheses to indicate the boundaries of the definition for the interpreter, who needs to "understand" what exactly you want to call. It is clear that after such an expression, access to the function will be lost, because it has not been saved anywhere.
The name of the constant is just its name, while the function itself has no name. This is why such functions are called "anonymous" in programming. In other languages, anonymous functions are often called lambda functions. In JavaScript they are sometimes called that too.
Creating within another function
Since an anonymous function is an expression, we can define it anywhere in the program that allows expressions, e.g., in the body of another function!
It follows from the fact that a function definition is an ordinary expression that it can be passed to other functions as arguments and returned from other functions as values. We will talk about this in more detail when we study higher-order functions.
The use of anonymous functions greatly enhances the expressive power of the language, and you will soon see that. In JavaScript, anonymous functions form the backbone of any program. Functions that create functions, return functions, and accept functions as arguments are the main way to develop in JavaScript.

