Getting Things Started

Stumbled upon the following predicament:

function demoChain(name) {
const part1 = “”hi””;
return function () {
const part2 = “”there””;
return function () {
console.log(`${part1.toUpperCase()} ${part2} ${name}`);

};
};
}

de…


This content originally appeared on DEV Community and was authored by JPStupfel

Stumbled upon the following predicament:

function demoChain(name) {
  const part1 = ""hi"";
  return function () {
    const part2 = ""there"";
    return function () {
      console.log(`${part1.toUpperCase()} ${part2} ${name}`);

    };
  };
}

demoChain(""Dr. Stephen Strange"")()(); 

Why, in the above example, would you need to include the

"()()" 

when invoking the function

demoChain(""Dr. Stephen Strange"")()(); 

??????

The answer:

Because demoChain()returns a function, the portion of the above code demoChain(""Dr. Stephen Strange"") is equal to an anonymous function. Therefore, just as writing demoChain in the console wouldn't actually run our code, writing demoChain(""Dr. Stephen Strange"") doesn't actually run our code. You have add the additional () after demoChain(""Dr. Stephen Strange"") in order to execute the function that demoChain(""Dr. Stephen Strange"") is equal to.

Likewise, demoChain(""Dr. Stephen Strange"")() actually equals a function, so we need to place a second pair of () after it to run.


This content originally appeared on DEV Community and was authored by JPStupfel


Print Share Comment Cite Upload Translate Updates
APA

JPStupfel | Sciencx (2022-04-06T21:28:55+00:00) Getting Things Started. Retrieved from https://www.scien.cx/2022/04/06/getting-things-started/

MLA
" » Getting Things Started." JPStupfel | Sciencx - Wednesday April 6, 2022, https://www.scien.cx/2022/04/06/getting-things-started/
HARVARD
JPStupfel | Sciencx Wednesday April 6, 2022 » Getting Things Started., viewed ,<https://www.scien.cx/2022/04/06/getting-things-started/>
VANCOUVER
JPStupfel | Sciencx - » Getting Things Started. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/06/getting-things-started/
CHICAGO
" » Getting Things Started." JPStupfel | Sciencx - Accessed . https://www.scien.cx/2022/04/06/getting-things-started/
IEEE
" » Getting Things Started." JPStupfel | Sciencx [Online]. Available: https://www.scien.cx/2022/04/06/getting-things-started/. [Accessed: ]
rf:citation
» Getting Things Started | JPStupfel | Sciencx | https://www.scien.cx/2022/04/06/getting-things-started/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.