0

JS functions learning

What about nested functions and function literals?

16th Mar 2017, 9:04 PM
Š’Š°Š»ŠµŃ€ŠøŠ¹ Š§ŠŗŠ°Š»Š¾Š²
Š’Š°Š»ŠµŃ€ŠøŠ¹ Š§ŠŗŠ°Š»Š¾Š² - avatar
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.
16th Mar 2017, 10:27 PM
John Afolayan