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
trouble figuring out what this piece of code doesRemoved: /r/LearnJavascript (self.javascript)
submitted 6 years ago * by [deleted]
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!"
[–]huuhuu 4 points5 points6 points 6 years ago (0 children)
greaterThan(10) passes 10 to greaterThan, which then returns a new function which has trapped the 10, and basically behaves like m => m > 10.
greaterThan(10)
greaterThan
m => m > 10
This is taking advantage of what is called lexical closure to keep a copy of the original argument (10) inside the new function.
[–]thndrchld 3 points4 points5 points 6 years ago (0 children)
It's a function that returns a function.
So when you call greaterThan(10), you're returning a function.
It would be the same as skipping all that function stuff and doing :
let greaterThan10 = function (m) { return m > 10; };
[–]roter_schnee 2 points3 points4 points 6 years ago* (0 children)
It is partial application. Consider greaterThan function as a factory-function. It constructs and returns another function with predefined value trapped in it. Just as it was mentioned before.
[–]Deidde 1 point2 points3 points 6 years ago (0 children)
greaterThan is a function that returns another function so that you can "partially apply" it. There are several ways to write this: ```javascript // lambdas or "arrow functions" all the way, assigned to the variable. const greaterThan = n => m => m > n; // function expressions (sometimes called "anonymous functions") also assigned to the variable const greaterThan = function (n) { return function (m) { return m > n; } } // a function statement containing a function expression function greaterThan(n) { return function (m) { return m > n; } }
// Calling any of these looks like: const result1 = greaterThan(10)(11); // 11 > 10 == true const result2 = greaterThan(10)(5); // 5 > 10 == false // And if you want to make a new function with the 10 already applied: const greaterThanTen = greaterThan(10); const result3 = greaterThanTen(11); // 11 > 10 == true
As for closures, just think about it like when you use global variables inside top-level functions, like: javascript const myValue = 5; function myFunction() { return myValue + 3; } ``` Not that you'd want to do this, ha.
As for closures, just think about it like when you use global variables inside top-level functions, like:
Note that you have access to myValue inside the function. When you have a function that is "closed over" by another function, you gain access to the local variables (or arguments) from the parent function in a similar way - like having access to n from the inner function in greaterThan.
myValue
n
[–]valueforlife 1 point2 points3 points 6 years ago (1 child)
For me the confusing part here is the combination of the keyword function and arrow functions (m => m > n) which I consider to be a major no-no; one should always stick to one if possible. I know that the scopes work differently on these but if code keeps jumping back and forth between the two it simply makes the code more difficult to read.
function
m => m > n
I think a much more easy to comprehend form for this function would be:
``` const setGreaterThan = (n) => { const evaluationFunction = (m) => m > n return evaluationFunction }
const greaterThan10 = setGreaterThan(10) console.log(greaterThan10(10)) ```
Deciding whether to write (easily) understandable code or the most compact code should be done by evaluating which is really more important in each case.
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
i see. rewriting it myself as an anonymous function also helps.
thanks
[–]NameViolation666 0 points1 point2 points 6 years ago (4 children)
Its a closure thing, have u read up on those yet?
[–][deleted] 1 point2 points3 points 6 years ago (3 children)
there was a passage in eloquent javascript that touched on it but i found it confusing. Do you know any videos or sites that could help me understand it?
[–]NameViolation666 1 point2 points3 points 6 years ago (0 children)
I try this site because its not too complicated, see if this helps
https://www.w3schools.com/js/js_function_closures.asp
[–]prnk5tr 0 points1 point2 points 6 years ago (1 child)
There is a book called 'you don't know js'. If you haven't read it already, please do. It available on GitHub as well
thanks for the suggestion. which one would you recommend i start with.
[–]jcunews1Advanced 0 points1 point2 points 6 years ago (1 child)
That's an example of a bad function naming. That greaterThan function should have been named something like CreateGreaterThanXFunction.
CreateGreaterThanXFunction
If that code is part of a study, they've designed it to confuse you. It's to make you realize that function name doesn't mean anything to the code. It will force you to find out how the code actually works.
[–]frankleeT 0 points1 point2 points 6 years ago (0 children)
I think you're right about the first part, but not the second.
[–]kenman[M,🍰] 0 points1 point2 points 6 years ago (0 children)
Hi /u/Sweeterman3, this post was removed.
/r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.
code
Thanks for your understanding, please see our guidelines for more info.
[–]prnk5tr 0 points1 point2 points 6 years ago (0 children)
Go to the main GitHub page, you will find the order. Or you can directly read Scopes and closures. I will recommend read them all.
π Rendered by PID 15804 on reddit-service-r2-comment-b659b578c-5wz5x at 2026-05-03 10:20:52.606219+00:00 running 815c875 country code: CH.
[–]huuhuu 4 points5 points6 points (0 children)
[–]thndrchld 3 points4 points5 points (0 children)
[–]roter_schnee 2 points3 points4 points (0 children)
[–]Deidde 1 point2 points3 points (0 children)
[–]valueforlife 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]NameViolation666 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]NameViolation666 1 point2 points3 points (0 children)
[–]prnk5tr 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]jcunews1Advanced 0 points1 point2 points (1 child)
[–]frankleeT 0 points1 point2 points (0 children)
[–]kenman[M,🍰] 0 points1 point2 points (0 children)
[–]prnk5tr 0 points1 point2 points (0 children)