0
JS functions learning
What about nested functions and function literals?
1 Answer
+ 2
Function literals:
https://www.safaribooksonline.com/library/view/javascript-the-good/9780596517748/ch04s02.html
Simply put, nested functions are functions placed inside a function block. Therefore, they can only be accesed within the function block they are created in.
function sum(arr) {
function add(a, b) {
return a + b;
}
return arr.reduce(add);
}
sum([2,3,5,7,1]); // 18
add(2,3); // Throws an error
sum() adds all the values of an array with the help of a nested function add. Accessing the add function outside of the sum functions throws an error.