One Pager Cheat Sheet
- JavaScript is a function-based programming language that enables developers to use higher-order functions for abstraction and reducing code repetition.
- JavaScript supports
functional programming
, which allows us to utilise pure mathematical functions and treat functions asfirst-class citizens
. - JavaScript allows functions to be used as
first-class citizens
, meaning they can be assigned to variables, used as object attributes, stored in arrays and utilized as function arguments or returns, which allows for the development and use ofHigher-order functions
. - Functions in JavaScript, which are considered as first-class citizens, allows us to use higher-order functions to compose new Array transformations through
map
,filter
andreduce
. Higher Order Functions
inJavascript
enable us to use a function as a parameter to another function.- The
useFunction()
function accepts afunction_as_argument()
as a parameter, allowing it to manipulate array items. - Functions can return other functions, which allows us to create
Higher Order Functions
, as shown in the examplelogger()
in Javascript. - The returnFunction() function is a higher-order function
outputting
thesecondFunction()
that requires an additional parametery
. - You can write functions that accept other functions as parameters, as well as
return
them asoutputs
. - The higher-order function,
useReturnFunc()
, returns a function,secondFunction()
, which takes an argument as a string, when given a function,fn()
, as an input. - Higher-order functions can be used to
abstract over
actions as well as values. - The output of the code is A due to the use of the higher-order function,
filterArr
, with a loop topush
elements greater thanm
into a new array, which is then logged asElements that are greater than m = 9 are: 10,25
. - A
higher-order function
filterArr()
generates and returns thegreaterThan()
function, which filters elements of an array larger than its inputm
. - Using a higher-order function to
transform
afunction
is an example of Functional Programming. - The
changeFn()
higher-order function accepts a function as an argument and passes all of the required arguments to it, returning1
when called with[ 3, 10, 25, 1, 7 ]
. Using a higher-order function
cancreate
a new control flow.- The provided code uses a custom
forEach
loop and anunless
function to check if each element in theintArr
array is even or odd, yielding10 is even 2 is even
as the output. - The
unless()
higher-order function enables users to create custom control flow based on a test condition. - JavaScript has numerous built-in higher-order functions, including
map()
,reduce()
,addEventListener()
,filter()
, andforEach()
, that offer substantial abstraction. - The
Array.prototype.filter()
method creates a filtered shallow duplicate of the array that contains only elements that pass a specific test. - Higher-order functions in JavaScript are powerful tools that allow for code to be communicated through actions and improved reusability, while providing the flexibility to use built-in or customized functions to handle complex tasks.