Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 6, 2022 09:28 pm GMT

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}`);    };  };}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.


Original Link: https://dev.to/jpstupfel/getting-things-started-439m

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To