Functions in JavaScript are the basic building blocks of the language that allow you to develop complex and flexible programs. Let's figure out what functions are, how to use them, and give many examples — from simple to complex — so that you can master this powerful concept and apply it in practice.

What are functions in JavaScript? 🤔
A function is a block of code that performs a specific task or returns a result. Functions allow you to reuse code, avoid repetitions, and make programs more readable and structured. In JavaScript, functions can be either named or anonymous, arrow, or higher-order functions.
Basics of creating functions
To create a function in JavaScript, use the function keyword followed by the function name, parameters in brackets, and the function body in curly brackets:
function greet() {
console.log("Hello, world!");
}
greet(); // Output: Hello, world!In this example, a simple function greet is created, which outputs the message "Hello, world!" to the console. To call the function, simply use its name, adding parentheses.
Parameters and return values 📝
Functions can take parameters and return values. This makes them more flexible.
function add(a, b) {
return a + b;
}
const result = add(5, 3);
console.log(result); // Conclusion: 8Here, the add function takes two parameters a and b and returns their sum. Using return allows you to return the result of the function.
Functional expressions 💡
Functions in JavaScript can also be created as expressions and assigned to a variable:
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(4, 7)); // Conclusion: 28Here we create an anonymous function (without a name) and assign it to the variable multiply. This function is called a functional expression.
Arrow Functions ➡️
Arrow functions are a shortened syntax for creating functions, especially useful when using callback functions.
const subtract = (a, b) => a - b;
console.log(subtract(10, 3)); // Conclusion: 7Arrow functions are shorter and can be more convenient. If the body of the function consists of a single expression, the return operator can be omitted, as in the example above.
Higher-order functions 🚀
Functions in JavaScript can take other functions as parameters or return functions. Such functions are called higher-order functions.
function applyOperation(a, b, operation) {
return operation(a, b);
}
const result1 = applyOperation(5, 10, (x, y) => x + y);
console.log(result1); // Conclusion: 15
const result2 = applyOperation(10, 3, (x, y) => x * y);
console.log(result2); // Conclusion: 30The applyOperation function takes the numbers a, b and the operation function, which determines what to do with these numbers. This is a powerful way to make the code flexible and easily configurable.
Recursive functions 🔄
A function can call itself. This is called recursion. Recursion is useful for solving problems that can be divided into simpler subtasks.
function factorial(n) {
if (n === 1) return 1;
return n * factorial(n - 1);
}
console.log(factorial(5)); // Conclusion: 120In this example, the factorial function recursively calls itself to calculate the factorial of the number n.
Closures 🔒
Closures are a powerful JavaScript feature that allows a function to remember its lexical environment even after it has been called.
function createCounter() {
let count = 0;
return function() {
count++;
return count;
};
}
const counter = createCounter();
console.log(counter()); // Conclusion: 1
console.log(counter()); // Conclusion: 2The createCounter function returns an internal function that has access to the count variable, even after the completion of createCounter.
Example of a complex function: Currying 🍛
Currying is a technique where a function is broken down into several functions, each of which takes one argument.
function multiply(a) {
return function(b) {
return function(c) {
return a * b * c;
};
};
}
console.log(multiply(2)(3)(4)); // Conclusion: 24This approach is useful when you need to gradually apply arguments to a function.
Example of a complex function: Using higher-order functions and closures 🚀
function createMultiplier(multiplier) {
return function(value) {
return value * multiplier;
};
}
const double = createMultiplier(2);
const triple = createMultiplier(3);
console.log(double(5)); // Conclusion: 10
console.log(triple(4)); // Conclusion: 12Here, the createMultiplier function creates new functions that can multiply values by a certain multiplier. This is an example of using closures and higher-order functions to create flexible and reusable components.
Conclusion 🎉
Functions in JavaScript are one of the most powerful tools that allow you to create structured, reusable, and readable programs. They help organize code, avoid duplication, and add flexibility to your solutions. From simple calls to complex combinations of closures and currying, the possibilities of using functions are endless.
Practice with different types of functions, and you will soon notice how your JavaScript skills become more advanced and effective! 💪
