Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7952683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:56:04+00:00 2026-06-04T02:56:04+00:00

I am working my way through Eloquent Javascript and I came across a code

  • 0

I am working my way through Eloquent Javascript and I came across a code snippet that looks like this:

function greaterThan(x) {
  return function(y) {
    return y > x;
  };
}

var greaterThanTen = greaterThan(10);
show(greaterThanTen(9));

Is there any real use-case to defining the function like this with an anonymous function inside of it? Wouldn’t it be much simpler to do this:

function greaterThan(x,y){
  return x > y;
}

var greaterThanTen = greaterThan(9, 10);

any ideas/comments/suggestions would be very helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:56:06+00:00Added an answer on June 4, 2026 at 2:56 am

    This is an example of a “closure”. Basically, a call to greaterThan() gives you a function. That function, instead of just a mere function, carries with it the value of x – it’s like embedding the value of x permanently into the function.

    function obj(x) {
    
        //"public interface"
        //only these are exposed (inc and getX). x is private.
        return {
            inc: function() {
                return ++x;
            },
            getX: function() { //a getter function to retrieve private x
                return x;
            }
        }
    }
    
    //the value 10 is "embedded" to the returned object
    var myobj = obj(10);
    
    //every call to inc increments that "embedded" value
    console.log(myobj.inc()); //11
    console.log(myobj.inc()); //12
    console.log(myobj.inc()); //13
    
    //but you can never access the variable of `x` directly without a "getter"
    console.log(myobj.getX());​
    

    Closures are one of the great features of JavaScript. One of it’s great uses is to emulate private variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working my way through Accelerated C++ and just came across this in
Im working my way through some C++ code and came across the following void
While working my way through the Android tutorials, I came across something I don't
I'm working my way through some code examples and I stumbled upon this: endings
I am working my way through some Objective-C code that I did not write
I'm working my way through the Eloquent JavaScript Book and in it there is
Still working my way through this program. Next task on my to-do list is
I'm currently working my way through the learning curve that is LINQ and I
I'm working my way through K&R's 2nd edition, and I've been stumped with this
I'm currently working my way through this book: http://www1.idc.ac.il/tecs/ I'm currently on a section

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.