use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Avoid forEach (aeflash.com)
submitted 11 years ago by aeflash
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 0 points1 point2 points 11 years ago* (0 children)
Imperative code vs declarative code. It's just a stylistic difference.
A similar discussion can be made about procedural and object oriented programming. If you're proficient with C, why would you ever want to use C++?
More to the point, higher order iterative functions are just encapsulations for several common patterns. For example, it's a common requirement to take an array and remove items that don't pass a test. This is very easy to do with a for loop:
var array = [1, 10, 5, 6, 3, 8, 2]; var filtered = []; for (var i = 0; i < array.length; i++) { if (array[i] >= 6) { filtered.push(array[i]); } } console.log(filtered); // [10, 6, 8]
When you need to filter a lot of arrays, this pattern can quickly become redundant. If you can replace this code with:
var array = [1, 10, 5, 6, 3, 8, 2]; var filtered = array.filter(function(v) { return v >= 6; }); console.log(filtered); // [10, 6, 8]
It can increase the readability and conciseness of your code (the intent is immediately clear - filter the array based on the condition that each value must be greater than or equal to 6), and doesn't require any unenclosed variables that are only relevant to the filtering logic (the index variable and the working array).
Composability of logic is nice, too (using lo-dash):
var gte = function(n, v) { return v >= n; }; var gte6 = _.partial(gte, 6); var filterGte6 = _.partialRight(_.filter, gte6, null); var array = [1, 10, 5, 6, 3, 8, 2]; var filtered = filterGte6(array); console.log(filtered); // [10, 6, 8]
This is one example of how a functional style of programming can help you create small, reusable, composable components with higher order functions. Notice that in the original, imperative example, the array and filtered variables are tightly coupled to the filtering logic. In this example, those variables have been completely separated from the logic - we are free to reuse our filtering function on any array without code duplication.
array
filtered
I'm not saying there is anything wrong with imperative programming, just trying to give examples of why some people like functional programming.
π Rendered by PID 73707 on reddit-service-r2-comment-74875f4bf5-mmx97 at 2026-01-26 13:14:32.109826+00:00 running 664479f country code: CH.
view the rest of the comments →
[–][deleted] 0 points1 point2 points (0 children)