0

function retirement(retirementAge) {
  var a = ' years left until retirement.';
  return function(yearOfBirth) {
    var age = 2016 - yearOfBirth;
    console.log((retirementAge - age) + a);
  }
}

var retirementUS = retirement(66);
retirementUS(1990);

My question is why yearOfBirth can be equal to retirementUS I understand this works I just don't understand why.

adiga
  • 34,372
  • 9
  • 61
  • 83
Noam Aris
  • 1
  • 1
  • 3
    *"My question is why `yearOfBirth` can be equal to `retirementUS`"* wait, who says that? they're not equal at all, one is a function and the other one is a number passed to that function. – Thomas Oct 09 '20 at 09:12

2 Answers2

0

the return value of retirement is a function. Since functions are „First-class citizens” in JavaScript, you can assign a function to a variable (or return it from another function).

You assign the return value to a variable retirementUS. This variable now holds a reference to the anonymous function function(yearOfBirth).

In the last line, you pass an argument into that anonymous function. Within that one, the argument gets referenced as yearOfBirth.

Since it is within the same scope as retirement, it has access to retirementAge.

Does that answer your question?

Ryuno-Ki
  • 692
  • 1
  • 6
  • 8
0
  1. In JavaScript functions are first-class citizens, you can assign a function to a variable.
  2. You retirement function is returning another function that you are assigning it to a variable you called retirementUS.

In your line:

var retirementUS = retirement(66);

You returned the result of the first function that took retirementAge as parameter and returned another anonymous function.

Then you called that anonymous function that you assigned to retirementUS:

retirementUS(1990);

You could also call both functions directly by:

retirement(66)(1990)

function retirement(retirementAge) {
  var a = ' years left until retirement.';
  return function(yearOfBirth) {
    var age = 2016 - yearOfBirth;
    console.log((retirementAge - age) + a);
  }
}

const retirementUS = retirement(60);

// your "retirement" returns another function
console.log(`Type of "retirementUS" is: ${typeof retirementUS}`)

// so you need to call that second function with its yearOfBirth parameter:

retirementUS(1990)
Fcmam5
  • 4,888
  • 1
  • 16
  • 33
  • I'm such a beginner so hard to grasp these concepts – Noam Aris Oct 09 '20 at 10:12
  • Hi @NoamAris, no worries. We are here to help :) So, I meant by **assign**: Put the return value of a function (it could be a number, a string or even another function) in a variable. And if we assign a function to a variable, we can use it(the variable) as a function as well. Please check this video for example: https://youtu.be/iZLP4qOwY8I, or just look-up for currying in JavaScript, if you still find it confusing check this great playlist: https://www.youtube.com/playlist?list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84 – Fcmam5 Oct 09 '20 at 10:20
  • If you have one equal sign, it means that you are assigning a value, and not checking for equality, and that what confuses people when they first read your question, so you should ask it like this: *My question is why yearOfBirth can be **assigned** (and not "equal") to retirementUS I understand this works I just don't understand why.* – Fcmam5 Oct 09 '20 at 10:22