one of the most important programming languages of all time, not simply because of its popularity, but because it popularized two features which are extremely important for the evolution of programming: Prototypal Inheritance & Functional Programming ”
Step by Step Define Problem & Transformation State Change Important Non-existent Order Of Execution Important Not as Important Flow Control Loop, Conditional Recursion
• No side effects • Given the same input, will always return the same output. • Relies on no external mutable state. Rule of Pure Function : impure function : function add(x,y){ var z = x + y } pure function : function add(x,y){ return x + y }
can : • store the function in a variable • have properties • pass the function as a parameter to another function • return the function from a function
as inputs and/or returns them as outputs. Function that create and return new function : When to use High-Order Function : • Asynchronous execution (Node.js, ajax) • Event Listeners/Handlers (jquery dll) • setTimeout and setInterval methods
temps to Celsius. var fahrenheit = [0, 32, 45, 50, 75, 80, 99, 120]; var celcius = fahrenheit.map(function(elem) { return Math.round((elem - 32) * 5 / 9); }); Formula : Use it when: You want to translate/map all elements in an array to another set of values.
elements from an array. var uniqueArray = array.filter(function(elem, index, array) { return array.indexOf(elem) === index; } ); Formula : Use it when: You want to remove unwanted elements based on a condition.
remove duplicate elements from an array. var sum = [0, 1, 2, 3].reduce(function(a, b) { return a + b; }, 0); // sum is 6 Formula : Use it when: You want to find a cumulative or concatenated value based on elements across the array.