When I worked as a Full-Stack developer, creating new modules using native Javascript (hereafter JS), and dealing with issues such as supporting the existing system gave me invaluable knowledge in practice. This has greatly helped me to understand what many of the Javascript constructs do in practice. From this article, we will share with you many of our experiences not only on the .NET platform but also on Javascript. These articles will give you a better understanding of the internal structure of JS.
function add(a, b) {
return a + b;
}
This JS article will be about callbacks, which many people struggle with. Callbacks are used a lot in practice. Even if you use a JS library, one of the most common JS topics you will use will be callbacks.
It all starts with functions….
The theme of functions in programming languages allows us to create reusable blocks of code. When using functions, you can ensure that a block of code is reused by passing parameters to them.
Here, if you give any value instead of arguments a and b, the program performs the correct calculation. With this approach, a kind of formula concept in mathematics is formed, and we get rid of repeatedly writing operations from the same classification. However, there are forms of functions to which it is not enough to simply pass a variable parameter. Sometimes it is necessary to pass blocks of code into smaller chunks to enable reuse and to handle this dynamically by passing one function as an argument to another function.
But what are the callbacks?
The callback is the process of passing another function as an argument to a function. Why would a function need to pass another function, or what does a callback do?
To answer this question, let's look at the simplest array search algorithm. Let's take a step-by-step look at how to perform an array search without resorting to JS's default search function.
Task: Given an array of numbers 45,56,103,91,405, 324. We need to find numbers greater than 50 in this array.
let array = [45, 56, 103, 91, 405, 324];
function findInArray(arr) {
let result = [];
for (let f of arr) {
if (f > 50) {
result.push(f);
}
}
return result;
}
After a while, it is necessary to search for elements in another part of the program on the same array (or it can be another array) with another condition (for example, finding numbers greater than 100 and less than 200). According to what we learned about this in algorithms, the code we write must respect the massiveness property of the algorithm. The convergence property states that an algorithm must be correct for solving all problems from the same group of problems (same classification). This means that if you are searching for an array under a certain condition, you should not write an algorithm from scratch when searching under another condition! The previous algorithm you wrote should already satisfy all the search conditions. But the above search function does not meet our requirements. Cause: The code being written is not extensible. Now let's ensure that this algorithm meets all search conditions.
First attempt
let array = [45, 56, 103, 91, 405, 324];
function findInArray(arr) {
let result = [];
for (let f of arr) {
if (f > 50) {
result.push(f);
}
}
return result;
}
Probability of code reuse: 0-10%
In our first attempt, we'll look at passing the number we're looking for as an argument to the function. Although it is easy to change the number, it is not possible to change the condition. Since we can change the number, let's take the percentage of code reuse as 0-10%.
Second attempt.. removing the argument of the conditional operator
let array = [45, 56, 103, 91, 405, 324];
function findInArray(arr, findNum, op) {
let result = [];
for (let f of arr) {
switch (op) {
case '>': {
if (f > findNum) {
result.push(f);
}
}
break;
case '<': {
if (f < findNum) {
result.push(f);
}
}
break;
case '>=': {
if (f >= findNum) {
result.push(f);
}
}
break;
}
}
return result;
}
We can achieve approximately 30-35% reuse of the code by removing the conditional operator as an argument, but this code is also very weak in terms of extensibility, "unreleasable" code. The reason is that if there are binary conditions (for example, finding numbers greater than 100 but less than 300), the function does not meet our requirements. Also, depending on the requirement, we will need to add new conditions. But how do we make sure that the written function can satisfy all the search demands?
It is time for callbacks or "Show me how you do!"
To be able to perform the problem by the mass property, it is necessary to remove the conditional block from the function and write it in the argument. But in the programming language, the if block cannot be passed directly to the function as an argument. To overcome this, it is necessary to extract the if block into a separate function and pass that function as an argument to the main function.









Join the conversation! Your thoughts help the community grow.