Register to get access to free programming courses with interactive exercises

First-class objects JS: Functions

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.

// 5 — - number, object of the first kind (stored in a constant)
const num = 5;

// 2 — number, object of the first kind (function argument)
// the contents of the num constant - first kind object (function argument)
// the content of the result constant is a first kind object (return value)
const result = Math.pow(num, 2);

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

// The function is written to a constant!
const x = () => console.log('I love Hexlet');

x(); // => 'I love Hexlet'

There's more action in this code than we're used to thinking:

  1. Creating (defining) a function: () => console.log('I love Hexlet')
  2. Creating a constant x and 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:

const a = () => console.log('I love Hexlet');

a(); // => 'I love Hexlet'

const b = a;

b(); // => 'I love Hexlet'

Moreover, any function can be used directly, without storing it in a constant:

(() => console.log('I love Hexlet'))(); // => I love Hexlet

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!

const sum = (a, b) => {
  // defined an "internal" anonymous function and
  // saved in the innerSum constant
  const innerSum = (x, y) => x + y;

  // called an internal function and
  // returned the result of the call to the outside of sum
  return innerSum(a, b);
};

sum(1, 4); // 5

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.


Recommended materials

  1. Docs

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY
Suggested learning programs
profession
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.