This is the 11th day of my participation in Gwen Challenge

The callback function

What is a callback function?

What’s the use of a callback function?

Let’s explore.

The so-called callback function refers to A function A is passed as an argument to another function B, and then function B calls function A inside the function body. In this case, function A is called A callback function. Anonymous functions are often passed as arguments to functions to implement callback functions.Copy the code

Let’s look at a specific example

<script> function cal(num1,num2,fn){ return fn(num1,num2); // A callback function lets the caller determine some of the functions in the function. } console.log(CAL (1,2,function(a,b){return a + b; // return a+b})); Console. log(CAL (1,2,function(a,b){return a * b; // return a*b}); </script>Copy the code

Arrays make use of callback functions

JavaScript provides many ways for arrays to perform specific functions using callback functions.

Find () returns the value of the first element of the tested array. Each element in the array calls the function execution once. When an element in an array returns true on a test condition, find() returns the element that matches the condition, and the subsequent value is not called. Returns undefined if there are no eligible elements. <script> var arr = [1,2,3,4,5]; function greater(num){ return num>3; } //find returns the first result that matches the condition. console.log(arr.find(greater)); // Result is 4 </script>Copy the code
Every () checks whether all elements in the array meet the criteria. If so, it returns true, otherwise false. If one element in the array is detected that does not meet the criteria, the entire expression returns false, and the remaining elements are not tested. <script> var arr = [1,2,3,4,5]; function greater(num){ return num>3; } //every checks if all elements meet the criteria, returns false if one element does not, returns true console.log(arr.every(greater)); // result is false </script>Copy the code
Some () checks for all elements in the array, returning true if any, false otherwiseCopy the code
ForEach () <script> var arr = [1,2,3,4,5]; var sum = 0; function add(num){ sum = sum + num; } //forEach executes the function arr.foreach (add) console.log(sum); // Result is 15 </script>Copy the code
Var arr = [1,2,3,4,5]; function fac(num){ return num * num; } // Return a new array with the values of the original array elements. console.log(arr.map(fac)); // The result is [1, 4, 9, 16, 25].Copy the code
Reduce () receives a function as an accumulator, and each value in the array (from left to right) begins to shrink and eventually evaluates to a single value. <script> var arr = [1,2,3,4,5]; Function sum(sumAll,num){return sumAll+num; } console.log(arr.reduce(sum)); // Result is 15 </script>Copy the code
The function of reduceRight() method is the same as that of reduce(), but the difference is that reduceRight() adds the items in the array forward from the end of the array.Copy the code