From bc3f9561d0471a347bdd062e92a220c58fce62f5 Mon Sep 17 00:00:00 2001 From: ITGrusha Date: Fri, 25 Oct 2019 19:57:51 +0300 Subject: [PATCH] High Order Functions tutorial Added basic information about high order functuions --- .../higher_order_functions.md | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/docs/JavaScript_Basics/higher_order_functions.md b/docs/JavaScript_Basics/higher_order_functions.md index 8768c4a..84e605a 100644 --- a/docs/JavaScript_Basics/higher_order_functions.md +++ b/docs/JavaScript_Basics/higher_order_functions.md @@ -1 +1,26 @@ -# Higher Order Functions \ No newline at end of file +# Higher Order Functions + +Higher-order functions are functions that take other functions as arguments or return functions as their results. + +Taking an other function as an argument is often referred as a callback function, because it is called back by the higher-order function. This is a concept that Javascript uses a lot. + +For example, the map function on arrays is a higher order function. The map function takes a function as an argument. + +```javascript +const list = [1, 2, 3, 4, 5 ]; +const double = n => n * 2 + +[1, 2, 3, 4].map(double) // [ 2, 4, 6, 8, 10 ] +``` + + +Or, with an anonymous function: +```javascript +const newList = list.map(function (item, index, list) { + return item * 2; +}); +// [ 2, 4, 6, 8, 10 ] +``` +The map function is one of the many higher-order functions built into the language. sort, reduce, filter, forEach are other examples of higher-order functions built into the language. + +Higher-order functions allows you to write simpler and more elegant code.