DEV Community

Cover image for ๐Ÿ”ฅ Advanced JavaScript Concepts.๐Ÿ˜Ž
Shaik Dawood
Shaik Dawood

Posted on • Updated on

๐Ÿ”ฅ Advanced JavaScript Concepts.๐Ÿ˜Ž

Hello everyone,๐Ÿ‘‹

If you are new to JavaScript, it can be little bit of challenging to understand some core and Advance topics ๐Ÿคทโ€โ™‚๏ธ. But don't worry keep your fingers crossed๐Ÿคž because in this article you'll get full information regarding Advanced JS Topics๐Ÿ˜.

โšก 1. Closures:

๐Ÿ‘‰ MDN Def: A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).

Doesn't sound great to you???๐Ÿค” Don't worry. Stay tuned.๐Ÿ”ฅ

So, Basically a function bind together with its lexical environment or lexical scope is known as Closer. Let's see what exactly is lexical scope.
closure1

โšกThe word lexical refers to the fact that lexical scoping uses the location where a variable is declared. Nested functions have access to variables declared in their outer scope.

closure2

โšก Running this code has exactly the same effect as the previous example of the x() function above.
โšก What's different (and interesting) is that the displayName() inner function is returned from the outer function before being executed. The reason is that functions in JavaScript form closures. A closure is the combination of a function and the lexical environment within which that function was declared.

โœ… Every closure has three scopes:

  • Local Scope (Own scope).
  • Outer Functions Scope.
  • Global Scope.

โœ… Let's say a function can access some variables of it's lexical environment or parent scope, the function actually remembers the reference of variable but it's value.

Let's look at the code:

closure3

Now you might have a bit understanding of Closures right.๐Ÿ˜Ž

โœ… let's see some of uses of Closures:

  • Module design pattern.
  • Currying.
  • Functions like once.
  • Memoize.
  • Maintaining state in Async World.
  • setTimeouts.
  • Iterators.
  • and so on.......!.

{ .....
..
... to be continued.
}

Top comments (0)