0

I've come across this code and not sure why X works here.

function event() {
  var eventS = 'triathlon';
  eventOfTheDay(eventS);
}

function eventOfTheDay(x) {
  return console.log('You set a reminder about ' + x);
}
event();
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
JT Spades
  • 13
  • 1
  • Function argument vs parameter. – morganney Dec 11 '21 at 15:11
  • 1
    `x` is just the parameter name. Regardless of what is passed into that function when it is called, it is accessible within that function as `x`. – Sean Dec 11 '21 at 15:11
  • [What is a callback function?](https://stackoverflow.com/q/824234) | [How to explain callbacks in plain english? How are they different from calling one function from another function?](https://stackoverflow.com/q/9596276) – VLAZ Dec 11 '21 at 15:11
  • You are passing in the ***value*** of `eventS`. The name of that variable is completely irrelevant to the function definition which would work exactly the same if you did `eventOfTheDay('triathlon');` – charlietfl Dec 11 '21 at 15:17
  • Just to clarify, x is the parameter of eventOfTheDay, but its argument is passed when eventOfTheDay is called inside the event() after event() gets executed? – JT Spades Dec 11 '21 at 15:54
  • Not after `event()` gets executed. As part of the execution of `event()`. `event()` calls `eventOfTheDay()`, and the execution of `event()` is over only once `eventOfTheDay()` finishes. – Sean Dec 11 '21 at 16:17

1 Answers1

2

The one and only thing about functions in a programming language is that the parameter name can be any proper variable name according to its language's convention, and it is not mandatory to be the same variable name that you have used in your calling function.

A simple example:

You give some "thing" to your friend. You call it as "X" and your friend can call it as "Y". The thing is same. Its just the name that you and your friend are using according to your wish. The same is with functions :)

In your example, you are passing 'triathlon'. You call it as eventS (Maybe you like to use it as eventS there because it makes sense there), and your friend (eventOfTheDay) calls it as x, or y or anything he wishes to (Because that new name can make sense there).

Deepak
  • 2,660
  • 2
  • 8
  • 23